Delete
list1 = [0,'A',1,2,3,4]
print( 'pop, The last number will be returned by default after deletion ' ,list1.pop())
print( 'pop, After deletion, the specified subscript value is returned by default ',list1.pop( 3))
print( 'remove, Delete the specified value ' ,list1.remove( 'A' ))
print(list1)
Return results :
pop, The last number will be returned by default after deletion 4
pop, After deletion, the specified subscript value is returned by default 2
remove, Delete the specified value None
[0, 1, 3]
section
list1 = [0,'A',1,2,3,4]
print(' tangent : ' ,list1[0:5],', Cut from beginning to end : ' ,list1[0:],)
print(' Inverse tangent : ' ,list1[-5:-2])
print(' step : ',list1[0:5:2],list1[ -5:-2:2])
print(' Specify inversion : ',list1[5:3:-1],', Reverse all :',list1[ ::-1])
Return results :
tangent : [0, 'A', 1, 2, 3] , Cut from beginning to end : [0, 'A', 1, 2, 3, 4]
Inverse tangent : ['A', 1, 2]
step : [0, 1, 3] ['A', 2]
Specify inversion : [4, 3] , Reverse all : [4, 3, 2, 1, 'A', 0]
increase
list1 = [4,0,1,1,2,3,4]
list2 = ['a',34]
print( ' add to ',list1.append('a'),list1)
print(' Add data at specified location ',list1.insert(1, 'a' ),list1)
Return results :
add to None [4, 0, 1, 1, 2, 3, 4, 'a']
Add data at specified location None [4, 'a', 0, 1, 1, 2, 3, 4, 'a']
Sort , Statistics
list1 = [4,0,1,1,2,3,4]
list2 = ['a']
print(' Count ',list1.count(1))
print(' Find the subscript ',list1.index(1))
print(' List splicing , There is no need to reassign after splicing ' ,list1.extend(list2) ,list1)
print(' Default sort , It is not supported to include both characters and numbers ',sorted(list1))
print(' In reverse order ',sorted(list1,reverse=True))
print(' Sort , Characters not supported ',list1.sort(),list1)
print(' In reverse order ',list1.sort(reverse=True) ,list1)
Return results :
Count 2
Find the subscript 2
List splicing , There is no need to reassign after splicing None [4, 0, 1, 1, 2, 3, 4, 'a']
Default sort , It is not supported to include both characters and numbers [0, 1, 1, 2, 3, 4, 4]
In reverse order [4, 4, 3, 2, 1, 1, 0]
Sort , Characters not supported None [0, 1, 1, 2, 3, 4, 4]
In reverse order None [4, 4, 3, 2, 1, 1, 0]
reversed reverse
aa=' One, two, three '
print(list(reversed(aa)))
aa=(' first place ',' Second ',' Third ')
print(tuple(reversed(aa)))
aa=[' first place ',' Second ',' Third ']
print(list(reversed(aa)))
Be careful :reversed() Function returns an iterator , Support string , Tuples , list ,
Return an object ,, So we need to list/tuple Function to convert the corresponding data type
Return results :
[' 3、 ... and ', ' Two ', ' One ']
(' Third ', ' Second ', ' first place ')
[' Third ', ' Second ', ' first place ']
List expression
list=[i*'n' for i in range(6)]
# amount to
list=[]
for i in range(10):
list.append(i*'n')
print(list)
Return results :
['', 'n', 'nn', 'nnn', 'nnnn', 'nnnnn']