To create a list:
1Name_list = ['python','Java','Shell','C + +']2 #or3Name_list = List (['python','Java','Shell','C + +'])
Slices: ( slice: Take multiple elements)
1A = [0,1,2,3,4,5,6]2 Print(A[0])#to access the elements in the list by subscript, the index is counted starting at 03 Print(A[-2])#can also be taken from the back, the last one represents -14 Print(A[1:5])#start from subscript 1 to fifth, including 1, not including 55 Print(A[0:4])#Ibid., including 0, excluding 46 Print(A[:4])#ditto, from the beginning, 0 can not write7 Print(A[2:5])#take second place to fifth place, including 2, excluding 58 Print(A[3:-1])#Ibid., including 3, excluding -19 Print(a[3:])#when you want to take the last one, you can ignore not writeTen Print(A[0::2])#the following 2, which represents jumping values, interval an element One Print(A[::3])#Same as above, but spaced two elements, 0 can ignore not write
Additional:
1 A.append (' I'm new here ')2print(a)
Results: [0, 1, 2, 3, 4, 5, 6, ' I am the new one ']
Insert:
1 a.insert (3,' insert a data in front of 3 ')2print(a) 3 A.insert (5,' insert a data after 3 ')4print(a)
Results: [0, 1, 2, ' insert a data in front of 3 ', 3, 4, 5, 6, ' I am the new one ']
[0, 1, 2, ' insert a data in front of 3 ', 3, ' Insert a data after 3 ', 4, 5, 6, ' I am the new one ']
Modify:
1 a[3]=' It's time to substitute '2print(a)
Results: [0, 1, 2, ' should be replaced ', 3, ' Insert a data after 3 ', 4, 5, 6, ' I am the new one ']
Delete:
1 delA[3]#specify where to delete2 Print(a)3A.remove ('Insert a data after 3')#specify what to delete4 Print(a)5A.pop ()#Delete the last one by default, or you can enter the subscript to delete the specified6 Print(a)
Results: [0, 1, 2, 3, ' Insert a data after 3 ', 4, 5, 6, ' I am the new one ']
[0, 1, 2, 3, 4, 5, 6, ' I am the new one ']
[0, 1, 2, 3, 4, 5, 6]
Extended:
1 b =2,3 a.extend (b)4print(a)
Results: (1, 2, 3)
[0, 1, 2, 3, 4, 5, 6, 1, 2, 3]
Statistics:
1 Print (A.count (3)) # look at the integer 3 in List A There are several
Results: 2
sort:' data types are different and cannot be sorted together '
(Default order: Special symbols, numbers, uppercase letters, lowercase letters)
1 a.sort () # Positive order 2print(a)3# Flip 4print(a)
Results: [0, 1, 1, 2, 2, 3, 3, 4, 5, 6]
[6, 5, 4, 3, 3, 2, 2, 1, 1, 0]
Get subscript:
1 Print (a) 2 Print (A.index (2))
Results: [6, 5, 4, 3, 3, 2, 2, 1, 1, 0]
5
Copy:
1. Copy (light copy)
1names = ['Nihao','Zhangsan',['Zhang San','John Doe'],'Lisi','Wangwu']2_name =names.copy ()3 #only the parent list is copied. A sub-list is equivalent to specifying only one location and therefore follows the change. 4NAMES[3] ='Lisi2'5Names[2][0] ='Zhang Sanfeng'6 Print(names)7 Print(_name)8Results: ['Nihao','Zhangsan', ['Zhang Sanfeng','John Doe'],'Lisi2','Wangwu']['Nihao','Zhangsan', ['Zhang Sanfeng','John Doe'],'Lisi','Wangwu']
2. Copy of the Calling module (light copy)
1 ImportCopy#defining the Copy module2names = ['Nihao','Zhangsan',['Zhang San','John Doe'],'Lisi','Wangwu']3_name =copy.copy (names)4 #As with _name = Names.copy (), the results are not discussed. 5NAMES[3] ='Lisi2'6Names[2][0] ='Zhang Sanfeng'7 Print(names)8 Print(_name)
3, call the module, only copy the original content, the change does not copy the content. (Deep copy)
1 ImportCopy2names = ['Nihao','Zhangsan',['Zhang San','John Doe'],'Lisi','Wangwu']3_name =copy.deepcopy (names)4 #true copy, regardless of subsequent changes5 Print(names)6NAMES[3] ='Lisi2'7Names[2][0] ='Zhang Sanfeng'8 Print(names)9 Print(_name)Results: ['Nihao','Zhangsan', ['Zhang San','John Doe'],'Lisi','Wangwu']['Nihao','Zhangsan', ['Zhang Sanfeng','John Doe'],'Lisi2','Wangwu']['Nihao','Zhangsan', ['Zhang San','John Doe'],'Lisi','Wangwu']
List--python