List operations
List function:
>>> list (' Hello ') [' H ', ' e ', ' l ', ' l ', ' O ']
Change the list:
>>> x=[1,1,1]>>> x[1]=2>>> x[1, 2, 1]
To delete an element:
>>> names = [' Wu ', ' Li ', ' Zhao ', ' Qian ']>>> del names[1]>>> names[' Wu ', ' Zhao ', ' Qian ']
Shard Assignment:
>>> name = List (' perl ') >>> name[' P ', ' e ', ' r ', ' L ']>>> name[2:] = list (' ar ') >>> name[ ' P ', ' e ', ' a ', ' R ']
>>> num = [1,2,3,4,5]>>> num[1:4]=[] # starts with 1 bits but does not include 4 bits >>> num[1, 5]
Add a new element at the end of the Append list:
>>> lst = [1,2,3]>>> lst.append (4) >>> lst[1, 2, 3, 4]
Number of Count statistics elements:
>>> [' we ', ' have ', ' we ', ' a ', ' Dog '].count (' we ') 2
Extend extension list:
>>> a = [1,2,3]>>> B = [4,5,6]>>> A.extend (b) >>> a[1, 2, 3, 4, 5, 6]
Index to find the location of the first occurrence:
>>> sentence = [' I ', ' have ', ' a ', ' little ', ' dog ']>>> sentence.index (' Little ') 3
Insert inserts an object into the list:
>>> num = [1,2,3,5,6,7]>>> num.insert (3, ' four ') >>> num[1, 2, 3, ' Four ', 5, 6, 7]
Pop removes the list element, the last one by default:
>>> x = [1,2,3]>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>>> x[2]
Combined Append:
>>> x = [1,2,3]>>> x.append (X.pop ()) >>> x[1, 2, 3]
Remove removes the first occurrence from the list:
>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']>>> x.remove (' is ') >>> x[' to ', ' or ', ' not ', ' to ', ' Be ']
Reverse Reverse storage elements:
>>> x = [1,2,3]>>> x.reverse () >>> x[3, 2, 1]
Sort order:
>>> x = [4,6,2,1,7,9]>>> x.sort () >>> x[1, 2, 4, 6, 7, 9]
>>> X.sort (reverse=true) >>> x[9, 7, 6, 4, 2, 1]>>> x.sort (reverse=false) >>> x[1, 2, 4 , 6, 7, 9]
Beginning Python from Novice to Professional (3)-list operation