list is similar to C + + in the Vector,c an array of languages used to store the sequential structure
Create a list ( List )
a=[' 1 ', ' 2 '] is enclosed in [] to denote a List, each element in the middle can be any type, separated by commas.
List structure
List function Splitting a string into a list
>>>list (' Chongshi ')
[' C ', ' H ', ' o ', ' n ', ' G ', ' s ', ' h ', ' I ']
Modifying a list : assigning values to an element
>>> x=[1,2,3,4]
>>> x[2]=18
>>> x
[1, 2, 18, 4]
Delete Element
>>> names = [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Sunliu ']
>>> del names[2]
>>> names
[' Zhangsan ', ' Lisi ', ' Sunliu ']
Shard Assignment:
>>> name =list (' Huzi ')
>>> Name
[' H ', ' u ', ' z ', ' I ']
>>> name[2:]=list (' Dazhi ')
>>> Name
[' H ', ' u ', ' d ', ' a ', ' Z ', ' h ', ' I ']
The content of name is "Huzi", starting with the 3rd character (2), replacing "Zi" with "Da" and being expanded "zhi", so the content of the new name is "Hudazhi".
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
List method
Object . Method (parameter)
1 , Append
The Append method is used to append a new object to the end of the list:
>>> ABC = [+]
>>> Abc.append (4)
>>> ABC
[1, 2, 3, 4]
2 , Count
Count method to count the number of occurrences of an element in a list:
>>>[' to ', ' being ', ' or ', ' not ', ' to ', ' being ',].count (' to ')
2
>>> x = [[1,2],1,1,[2,1,[1,2]]
>>> x.count (1)
2
>>> X.count ([up])
1
3, Extend
The Extend method can append multiple values from another sequence at the end of the list at once. To extend an existing list with a new list:
>>>a = [A]
>>> B = [4,5,6]
>>> A.extend (b)
>>> A
[1, 2, 3, 4, 5, 6]
4. Index
The index method is used to find the first occurrence of a value from a list.
>>>knights = [' We ', ' is ', ' the ', ' kninghts ', ' who ', ' say ', ' ni ']
>>> Knights.index (' Who ')
4
>>> Knights[4]
' Who '
5. Insert
The Insert method is used to insert an object into the list:
>>> numbers = [1,2,3,5,6,7]
>>> Numbers.insert (3, ' four ') # inserted in front
>>> numbers
[1, 2, 3, ' Four ', 5, 6, 7]
6. Pop
The Pop method removes an element from the list (the last one by default) and returns the value of the element:
>>>x = [A]
>>> X.pop ()
3
>>> x
[1, 2]
>>> X.pop (0)
1
>>> x
[2]
Analog compression, stack
>>>a=[]
>>> a.append (0) # Press Stack
>>>a.append (1)
>>>a.append (2)
>>>a
[0, 1, 2]
>>> A.pop () # out of the stack
2
>>>a.pop ()
1
>>>a.pop ()
0
Analog Queues
>>> A.insert (0,0) # Queue
>>>a.insert (0,1)
>>>a.insert (0,2)
>>>a
[2, 1, 0]
>>> A.pop () # out Team
0
>>>a.pop ()
1
>>>a.pop ()
2
>>>a
[]
Or:
>>> a.append (0) # Queue
>>>a.append (1)
>>>a.append (2)
>>>a
[0, 1, 2]
>>> a.pop (0) # out Team
0
>>>a.pop (0)
1
>>>a.pop (0)
2
>>>a
[]
7. Remove
The Remove method is used to remove the first occurrence of a value in the list: # no return value, opposite of pop
>>>x = [' to ', ' is ', ' or ', ' not ', ' to ', ' being ']
>>> x.remove (' be ')
>>> x
[' to ', ' or ', ' no ', ' to ', ' being ']
8, Reverse
The Revers method stores the elements in the list in reverse
>>>x = [A]
>>> X.reverse ()
>>> x
[3, 2, 1]
This can also be achieved:
>>> List (reversed (x))
[3, 2, 1]
9. Sort
The sort method is used to sort the list at the original location. "Sort in place" changes the original list so that the elements can be sorted in a certain order (no return value).
>>>x = [4,6,2,1,7,9]
>>> X.sort ()
>>> x
[1, 2, 4, 6, 7, 9]
>>> x = [4,6,2,1,7,9]
>>> y = x # copy sort
>>> Y.sort ()
>>>y
[1, 2, 4, 6, 7, 9]
# another way to sort replicas
>>>x = [4,6,2,1,7,9]
>>> y = sorted (x)
>>>y
[1, 2, 4, 6, 7, 9]
>>>x
[4, 6, 2, 1, 7, 9]
Sorted can be used for any sequence
>>>sorted (' python ')
[' H ', ' n ', ' o ', ' P ', ' t ', ' Y ']
Ten , Advanced sorting
List de-weight: {}.fromkeys (list). Keys () List character sort: List.sort (key=int) list.sort (Key=len) #指定排序键类型 l Ist.sort (reverse=true) #反向排序
From for notes (Wiz)
3. List method