[Python 3 Series] List

Source: Internet
Author: User
Tags sorts stdin

Lists and tuples can contain multiple values. Also, because the list itself can contain other lists, you can use them to arrange the data in a hierarchical structure.


List data type

A "list" is a value that contains a sequence of multiple words.

[] is an empty list that does not contain any values, similar to an empty string '.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9B/52/wKioL1lhvG3SEWrZAAAjepjEJL4616.png "title=" 1.png "alt=" Wkiol1lhvg3sewrzaaajepjejl4616.png "/>

You can use the following table to obtain a single value from the list. The index of the first value in the list is 0, the second value is subscript 1, and so on.

In the negative subscript, the integer value-1 refers to the last subscript in the list, 2 refers to the second lowest subscript in the list, and so on. such as: Sample[8],sample[-1], in which the values are 9.

If the subscript used exceeds the number of values in the list, Python will error indexerror.

The subscript can be an integer only and cannot be a floating-point number.

A list can also contain other list values. The values in these lists can be accessed through multiple subscripts.

>>> list=[[' A ', ' B ', ' C '],[1,2,3], ' abc ',123]>>> list[1][2]3


Slices can get multiple values from the list, and the result is a new list.

The slice grows upward until the second subscript value, but does not include it.

Omitting the first subscript is equivalent to using 0, or the beginning of a list.

Omitting the second subscript is equivalent to using the length of the list, which means that the Shard is at the end of the list.

>>> list[0:2][[' A ', ' B ', ' C '], [1, 2, 3]]>>> list[:2][[' A ', ' B ', ' C '], [1, 2, 3]]>>> list[:][[' A ', ' B ', ' C '], [1, 2, 3], ' ABC ', 123]


Use the Len () function to get the length of the list.

>>> len (list) 4>>> len (List[0]) 3


You can use the subscript to change the values in the list.

>>> list[3]= ' newvalue ' >>> list[[' A ', ' B ', ' C '], [1, 2, 3], ' abc ', ' NewValue ']>>> list[0][2]= ' Newc ' >>> list[[' A ', ' B ', ' NEWC '], [1, 2, 3], ' abc ', ' NewValue ']


The "+" operator can connect two lists and get a new list.

The "*" operator can be used for a list and an integer to make a copy of the list.

>>> [1,2,3]+[' A ', ' B ', ' C '][1, 2, 3, ' A ', ' B ', ' C ']>>> [' an ', ' B ']*5[' A ', ' B ', ' A ', ' B ', ' A ', ' B ', ' A ', ' B ', ' A ', ' B ']


The DEL statement deletes the value at the bottom of the list, and all values following the deleted value in the table will move one subscript forward.

>>> newlist[1, 2, 3, ' A ', ' B ', ' C ']>>> del newlist[-2]>>> newlist[1, 2, 3, ' A ', ' C ']


Working with lists

Use Range (len (somelist)) in the For loop to iterate through each subscript of the list.

>>> for I in range (len (newlist)): ... print (newlist[i]) ... 123ac


With the in and not in operators, you can determine whether a value is in the list.

>>> ' A ' in newlisttrue>>> ' C ' not in Newlistfalse


The "+ =" operator can complete the connection of strings and lists.

The "*=" operator can complete a copy of the string and 1.

>>> list= ' Hello ' >>> list + = ' World ' >>> list ' Hello World ' >>> newlist=[' A ', ' B ']> >> newlist *=3>>> newlist[' A ', ' B ', ' A ', ' B ', ' A ', ' B ']


Method

Methods and functions are one thing, except that it is called on a value.

Each data type has its own set of methods.


The index () method, which can pass in a value, returns its subscript if the value exists in the list. If it does not exist, the error valueerror.

If there is a duplicate value in the list, it returns the subscript it first appears on.

>>> newlist[' A ', ' B ', ' A ', ' B ', ' A ', ' B ']>>> newlist.index (' B ') 1


The Append () method allows you to add parameters to the end of the list.

The Insert () method to insert a value at any subscript of the list.

The append () and insert () methods are list methods that can only be called on a list and cannot be called on other values.

>>> newlist.append (' cat ') >>> Newlist.insert (3, ' dog ') >>> newlist[' A ', ' B ', ' A ', ' dog ', ' B ', ' A ', ' B ', ' Cat ']


Passes a value to the Remove () method, which is removed from the called list.

Attempting to delete a value that does not exist in the list causes a valueerror error.

If the value appears more than once in the list, only the first occurrence of the value is deleted.

The DEL statement works well if you know the subscript of the value you want to delete in the list.

If you know the value that you want to remove from the list, the Remove () method is useful.

>>> newlist.remove (' dog ') >>> newlist[' A ', ' B ', ' A ', ' B ', ' A ', ' B ', ' Cat ']


The sort () method is used for sorting. The sort () method sorts the list on the spot.

You cannot sort a list that has both numeric and string values.

The sort () method sorts strings by using ASCII characters.

If you need to sort by a normal dictionary order, set the keyword argument key to Str.lower.

You can specify the Reverse keyword argument to true to have sort () sorted in reverse order.

>>> sample[1, 3, 5, 7, 2, 4, 6]>>> sample.sort () >>> sample[1, 2, 3, 4, 5, 6, 7]>>> Sam Ple.sort (reverse=true) >>> sample[7, 6, 5, 4, 3, 2, 1]>>> sample=sample+[' abc ']>>> Sample.sort () Traceback (most recent): File ' <stdin> ', line 1, in <module>typeerror:unorderable Typ ES:STR () < int ()


Types of similar lists: strings and tuples

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9B/52/wKiom1lhvqfTm0TdAABRDxbL5Tg086.png "title=" 2.png "alt=" Wkiom1lhvqftm0tdaabrdxbl5tg086.png "/>

Strings and lists are actually similar, and can be thought of as a list of individual text characters. For many operations on a list, you can also use it for strings, to mark values, to slice, for a for loop, for Len (), and for in and not in operators.

A list is a "mutable" data type whose value can be added, deleted, or changed. However, the string is "immutable" and it cannot be changed.

The correct way to "change" a string is to use slices and joins. Constructs a "new" string, copying some parts from the old string.

>>> name= ' Big Mike ' >>> name[4:] ' Mike ' >>> newname= ' I am ' +name[4:]>>> newName ' I am Mike '


The tuple data type, which is the immutable form of the list data type.

Tuples are entered with parentheses () instead of square brackets [].

If there is only one value in the tuple, you can follow the value in parentheses followed by a comma, indicating that it is a tuple instead of a string.

>>> name= (' Big Mike ', ' Little Tom ', ' Good Jack ') >>> Name[1:2] (' Little Tom ',) >>> name[2]= ' bad Marry ' Traceback (most recent): File "<stdin>", line 1, in <module>typeerror: ' Tuple ' object does no T support Item Assignment


Use the list () and tuple () functions to convert the types of lists and tuples that return the list and tuple versions of the values that are passed to them.

>>> tuple ([3]>>>]) (1, 2, 3) >>> list ((+)) [1, 2, list (' Hello ') [' H ', ' e ', ' l ', ' l ', ' O ' ]


Reference

A list reference is a value that points to a list.

When a list is created, it is referenced to a variable.

A variable is like a box containing a value.

When a function is called, the value of the parameter is copied to the argument. For a list, this means that the variable gets a copy of the reference.

>>> a=11>>> b=a>>> a=100>>> a100>>> b11>>> c=[1,2,3,4]>> > d=c>>> c[1]=5>>> c[1, 5, 3, 4]>>> d[1, 5, 3, 4]


The Copy.copy () function, which can be used to copy mutable values such as lists or dictionaries, and not just copy references.

If the list you want to copy contains a list, use the copy.deepcopy () function instead. The Deepcopy () function copies the list of colleagues inside them.

>>> import copy>>> list=[' A ', ' B ', ' C ']>>> newlist=copy.copy (list ) >>> newlist[1]=100>>> list[' A ',  ' B ',  ' C ']>>> newlist[' a ',  100,  ' C ']>>> list=[1,2,3,[4,5,6,[7,8,9]]]>>> newlist1=copy.copy (list) >>> newlist1[3][0]= ' abc ' >>> newlist1[1]= ' abc ' [1, 2, 3, [' abc ',  5, 6, [7, 8, 9]]]>>> newlist1[1, 2, 3, [' abc ', &NBSP;5,  6, [7, 8, 9]]]>>> list[1, 2, 3, [' abc ', &NBSP;5,&NBSP;6,  [7, 8, 9]]]>>> list=[1,2,3,[4,5,6]]>>> newlist1=copy.copy (list) >>> newlist2=copy.deepcopy (list) >>> list[2]=100>>> newlist1[1,  2,&NBSP;3,&NBSP;[4,&NBSP;5,&NBSP;6]]&GT;&GT;&GT;&NBSP;NEWLIST2[1,&NBSP;2,&NBSP;3,&NBSP;[4,&NBSP;5,&NBSP;6]] >>> list[3][0]=100>>> newlist1[1, 2, 3, [100, 5, 6]]>>> newlist2[1, &NBSP;2,&NBSP;3,&NBSP;[4,&NBSP;5,&NBSP;6]]


This article is from the "garbled Age" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1945641

[Python 3 Series] List

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.