python1.2

list of python

list = [ ]
list = [1,2,3,100,5,3] - list can be repeated
list[0]=1 list[1]=2 list[-1]=3 list[-2]=5
list.index (1)=0 list.index(3)=2---match the first
list.count(3) = 2 to see the number of duplicates
list[0:4] = [1,2,3,100]--- Slice (with head and tail)
list[-3:-1]=[100,5]
list[-3:]=[100,5,3]
list[:4]=[1,2,3,100]
list[ :]=[1,2,3,100,5,3]
list[-3::1]=[100,5,3] same as list[-3:]=[100,5,3]
list[-3 ::2]=[100,3]----- even, the last: n is the meaning of the step, that is to skip n-1 units
list.append() --- append, add
list at the end .insert(0,'a')=['a',1,2,3,100,5,3] --- Insert
list.insert(1,'b')=['a','b',1 ,2,3,100,5,3]
list[1]='c' list=['a','c',1,2,3,100,5,3]----modify
list.pop()-- Remove the last element [
1,2,3,100,5] list.remove(3)----remove the first 3, [1,2,100,5,3]
del list[2] deletes the 3rd element, the 3rd element is 3, [1,2,100,5,3]
del list deletes the list
del list[1:4] deletes the 2nd element to the 4th element [1 , 5, 3]

range(10) or range(0,10) is a list of 0 to 9
for i in range(10): print(i)

Positive order: list.sort() from small to large
Reverse order: list.reverse() from large to small

There are strings and numbers in the list that cannot be sorted, and an error is reported. The strings are sorted according to ASCII code from small to large

List concatenation: list.extend(list1) is the same as list+list1

Clear the list: list.clear()

list1=list (list1 points to list, not to the value of list)
Modify the element list1 of the list is modified at the same time

list2=list.copy() (list2 points to the value of the list, not to the list)
Modify the list element, list2 will not be modified

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324576671&siteId=291194637