List
Lists are frequently used data types in Python for storing multiple values.
Expressions: separated by commas within [], can hold n any type of value
eg
#Single Typeusers=['Yangli','Yite','Fixd']#multiple types, list nestingusers=['room01',['Yangli', 20,'Music'],['Yite', 20,'Basketball'],]
List common methods and functions
Method
Function
Common operations
eg
Len (list)
# len (list) calculates the length of the list ls = ['a','b','C ','d'= len (ls)print( Length)# results: 4
Append ()
#append () Appendls = ['a','b','C','D']ls.append ('x')Print(LS)#result: [' A ', ' B ', ' C ', ' d ', ' X ']#Append add an element to the end of the list
Inser (Index,obj)
#Inser (index,obj) Insert: Index inserts the start of the list (must be specified), the element to which obj is insertedls = ['a','b','C','D']ls.insert (0,'x')#Insert in headPrint(LS)#result: [' x ', ' A ', ' B ', ' C ', ' d ']ls= ['a','b','C','D']ls.insert, Len (LS),'x')#Insert at TailPrint(LS)#result: [' A ', ' B ', ' C ', ' d ', ' X ']ls= ['a','b','C','D']ls.insert (2,'x')#Insert obj before an element with the original list index of 2Print(LS)#result: [' A ', ' B ', ' X ', ' C ', ' d ']
Remove ()
# remove () removes the first occurrence of a value in the list, no return value ls = [ " a , " b , " c , " d " ]ls.remove ( " C ") # Remove C print (LS) # result: [' A ', ' B ', ' d ']
Pop (index)
#Pop (index) removes the first occurrence of a value in the list without a return valuels = ['a','b','C','D']res= Ls.pop ()#Delete the last element by defaultPrint(LS)#result: [' A ', ' B ', ' C ']Print(RES)#Result: D pop () returns the deleted elementls= ['a','b','C','D']res= Ls.pop (2)#Delete the last element by defaultPrint(LS)#result: [' A ', ' B ', ' d ']Print(RES)#Result: C pop () returns the deleted element
Reverse ()
# reverse () #反转列表元素ls = ['a','b',' C ','d']ls.reverse ()print(ls) # [' d ', ' C ', ' B ', ' a ']
operator of the list
The + and * operators are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists.
# + *LS1 = ['a', 2,'C']LS2= [1,'y','Z']LS3= LS1 +LS2Print(LS3)#results: [' A ', 2, ' C ', 1, ' Y ', ' z ']Print(ls1*3)#results: [' A ', 2, ' C ', ' A ', 2, ' C ', ' A ', 2, ' C ']
In similar to string in use
#In #判断元素是否存在列表中ls = ['a','b','C','D']Print('C' inchls#TruePrint('x' inchls#False
Comparison of lists ( prerequisites: Must be of the same type to be compared, otherwise error )
#Major premise: only the same type directly compare the size, for the index value of the direct comparison is based on the location one by one corresponding to the comparisonLS1 = ['a','b','C']LS2= ['x','y','Z']LS3= ['A','Z']Print(LS1 > LS2)#Result: FalsePrint(LS1 > LS3)#Result: True
I want to add ...
Slices of a list
For a slice of the list, do not repeat here, refer to the slice of the string.
Python full stack development: Python string slicing
Reading of the list
ls = ['a','b','C','D']#Dependent IndexesI=0 whileI <len (LS): #while循环Print(Ls[i]) I+=1 forIinchrange (len (LS)): #for循环Print(i, ls[i])#do not rely on indexes forIteminchls: #for循环Print(item)
Python Full stack development: List of Python lists