One, the list definition
Python list is one of Python's built-in data objects
The list is contained with "[]", with arbitrary data objects, each data object
With a "," split, each data pair is called an element
A python list is an ordered sequence.
Python list supports arbitrary nesting with no restrictions on nested hierarchy depth
A=[] #空列表print aa=["seq1", "seq2", ' Keyman ', 808,3.1] #创建列表print aa=list (' abcsgr ') #list函数创建列表print aa=[' A ', [' B ', ' C '], ' d '] #列表嵌套print a
Execution results are
[] [' seq1 ', ' seq2 ', ' Keyman ', 808, 3.1][' A ', ' B ', ' C ', ' s ', ' G ', ' r ' [' A ', [' B ', ' C '], ' d ']
Second, the list of Shard operations (cursors)
Lists are ordered sequences, support index operations, or search for indexes by element
The index of the list starts at 0, and so on
[' s ', ' t ', ' r ', ' I ', ' n ', ' G ', ' s '] The [0:-1:2] action method is the same as a string, and is returned as a list
[' s ', ' t ', ' r ', ' I ', ' n ', ' G ', ' s '] [0] Depending on the position of the index, find the corresponding element of the index, returned as a string
A=list (' Python,nihao ') print Aprint a[0:7:2]print a[3]
Execution results are
[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O '] [' P ', ' t ', ' o ', ', ']h
Third, List method
1.list.append (object)
Append is to append the new element to the list and append the position to the list at the end of the
Append the appended element can only be one element and cannot be manipulated simultaneously by multiple elements
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a.append ('! ') >>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ', '! ']
2.list.count (value)
Count the number of occurrences of each object in the object in the list
return value is number
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ', '! '] >>> Print A.count (' n ') 2>>> print a.count (' B ') 0
3.list.extend (iterable)
Expand the list to make the iterated object differential, append it to the list
Iterable refers to an object that can be iterated
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ', '! '] >>> a.extend (' hello! ') >>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ', '! ', ' h ', ' e ', ' l ', ' l ', ' o ', '! ']
4.list.index (value, [Start, [stop]])
Finds the index of an element based on the elements in the list
return value is number
Optional parameter start stop refers to what scope to look for (specific usage and string cursor operations)
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a.index (' a ') 10>>> a. Index (' h ', 5) 9>>> a.index (' h ', 4,9) Traceback (most recent call last): File "<pyshell#17>", line 1, in <mo Dule> a.index (' h ', 4,9) ValueError: ' H ' isn't in list>>> a.index (' h ', 4,10) 9>>>
5.list.insert (Index, object)
Inserts the corresponding object at the given index position, and the object can be arbitrary
The Insert method is equivalent to List[6:6]=object, not equivalent to List[6]=object
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a.insert (6, ' 6 ') >>> P Rint a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ' 6 ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a[6]= ' 7 ' >>> print a[' P ', ' Y ', ' t ', ' h ', ' o ', ' n ', ' 7 ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a[2:2]= ' d ' >>> a[' P ', ' y ', ' d ', ' t ', ' h ', ' O ', ' N ', ' 7 ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']
6.list.pop (Index)
Pop-up the element in the list and delete the element, the default is the last one
The index parameter specifies that the element corresponding to the specified index pops up
>>> print a[' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a.pop (3) ' H ' >>> print a[' P ', ' y ', ' t ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ', ' O ']>>> a.pop () ' O ' >>> print a[' P ', ' y ', ' t ', ' o ', ' n ', ', ', ' n ', ' I ', ' h ', ' A ']>>> a.pop (a.index (' y ')) ' Y ' >>> print a[' P ', ' t ', ' o ', ' n ', ', ', ' n ', ' I ', ' H ' , ' a ']
7.list.remove (value)
Deletes the specified element, which should be present in the list if the value does not exist to raise valueerror
>>> print a[' P ', ' y ', ' d ', ' h ', ' o ', ' n ', ' 7 ', ', ', ' n ', ' I ', ' h ', ' A ']>>> a.remove (' n ') >>> pri NT a[' P ', ' Y ', ' d ', ' h ', ' O ', ' 7 ', ', ', ' n ', ' I ', ' h ', ' A ']>>> a.remove (' P ') Traceback (most recent call last): F Ile "<pyshell#35>", line 1, in <module> a.remove (' P ') ValueError:list.remove (x): X not in List
8.list.reverse ()
Swap the elements in the list before and after the order
Reverse is not anti-sorting
>>> print a[' P ', ' y ', ' d ', ' h ', ' O ', ' 7 ', ', ', ' n ', ' I ', ' h ', ' A ']>>> a.reverse () >>> print a[' A ', ' h ', ' I ', ' n ', ', ', ' 7 ', ' O ', ' h ', ' d ', ' Y ', ' P ']
9.list.sort ()
Sort the elements within a list
The order of sorts is 0-9,a-z,a-z
Reverse=true Anti-sorting
>>> a=list (' afsdfbADGDSFSD214325325 ') >>> a.sort () >>> print a[' 1 ', ' 2 ', ' 2 ', ' 2 ', ' 3 ', ' 3 ', ' 4 ', ' 5 ', ' 5 ', ' A ', ' d ', ' d ', ' d ', ' f ', ' G ', ' s ', ' s ', ' A ', ' B ', ' d ', ' f ', ' f ', ' s ']>>> a.sort (reverse=true) >> ;> print a[' s ', ' f ', ' f ', ' d ', ' B ', ' A ', ' s ', ' s ', ' G ', ' f ', ' d ', ' d ', ' d ', ' A ', ' 5 ', ' 5 ', ' 4 ', ' 3 ', ' 3 ', ' 2 ', ' 2 ', ' 2 ' , ' 1 ']
10.list[index]=value and Del List[index]
List[index]=value: Assigning a value to a specified cursor in a list
Del List[index]: Removes the specified cursor value from the list
>>> a=list (' abc123456 ') >>> print a[' A ', ' B ', ' C ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']>>> a[3]= ' a ' > >> print a[' A ', ' B ', ' C ', ' A ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']>>> del a[3]>>> print a[' A ', ' B ', ' C ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']
This article is from the "Raffaele" blog, make sure to keep this source http://raffaele.blog.51cto.com/6508076/1569456
Python Learning note 3-python list