1, the list of basic operations Method 1, the list is one of the underlying data types in Python, other languages also have a list-like data type, such as JS called array, he is surrounded by [], each element is separated by commas, and he can hold a variety of data types such as:
You can take values based on indexes, slices, and steps, just as you do with string methods.
Li =['HHD','Rob a crossbow vv',2,'big guy .','to Anger','your Uncle',8,]print (li[5]) print (li[:5]) print (li[-1::-1]) #倒序取值print (li[1:6:2]) #可以加步长取值
2, increase, append, insert, Extend
li=['ll','888','jjhh',' like',' See',['Star','Galaxy','Universe'],8]# Li.append ('to learn'Append # Print (LI) # Li.insert at the end of the list (3,'Xian San'), insert can add # Print (LI) # Li.extend by index ('Yqeyey'to iterate, add each element to the smallest element and add a # print (LI)
Name_list = ['Traitors','of Christianity','it's all good.'] whileTrue:name=input ('Please enter the name of the new employee:') ifName.upper () = ='Q': Break Else: Name_list.append (name) print ('New employee%s was added successfully'%name) print (name_list)
3, delete, pop,, remove,,clear,,, del
li=['JFH','er','wer','Oh','Ask','wer',[' -','Add']] #li. Pop () #默认删除最后一个 #li.pop (1#根据索引删除, you can define a variable to accept the deleted value, and the pop delete return value #li.remove ('wer'#按照元素删除, the default is to delete only one, if there are two identical elements #li.clear () #清空列表中的数据, leave the empty list #del Li to delete the entire list #del li[:4] #del li[can be deleted based on the index tiles:7:2] You can also remove print (LI) with step size
4, change, change by index
Li =[' A',' -','ADN','ASD',['ci','Yu'],'wen6','Ash']# li[0]='9'#根据索引去改 (an element separated by commas) #li [2:5]='Hdshds'#把字符串的化成最小元素添加 #li[3]="' cozy ', ' yuan ', [' sxxzx '], ' 22uu '"Delete the original element and add it
5, check, according to index, slicing step to check, you can also for the loop to check
Li =[' A',' -','ADN','ASD',['ci','Yu'],'wen6','Ash']print (li[:5]) #根据切片和索引去查 forIinchLi:print (i)
6, other methods of operation
Li =[' A',' -','ADN','ASD','Qen6','Ash']#li.sort () #根据ascii sorting must be of the same data type #Li.sort (reverse=true) Reverse sort#li.reverse () FlipPrint(LI)
Li =[' A',' -','ADN','ASD','Qen6','Ash']Print(Li.count (' A'))#CountPrint(Li.index ('Ash'))#Indexed by ElementPrint(Len (LI))#List Length
7, Nesting of lists
# Li =[2, ' Zhen ', ' Xing ', 4,[' flower ', ' shh ', 66], ' people ']1,' capitalize the Xing's initials '2,' Put Shh all caps '3,' put 66 +1 back in place '# # Li[2]=li[2].capitalize () # # li[4][2]=li[4][2]+1# li[4][1] = Li[4][1].upper ()# Print (LI)
2, tuple read-only (), by index, slicing
Tuples are called read-only lists, where data can be queried but cannot be modified, so the slicing of strings also applies to tuples. Example: ("A", "B", "C").
If there are other data types in the tuple, you can also modify them.
Other Range,len.
for in Range (100,0,-1):# as a list of numbers, the range #(0,100,2) can also be added in step size, self-defined range, #(100,0,-1)-1 can also be inverted to value print(i)
Join: Use a string to make a connector that iterates over each element in the object, forming a new string
s='^'. Join (' Meizu beautiful phone ')print(s)
Basics of Python Learning fifth day how to base data types