List of common operations in python3.*

First define a list: 
names= ["xiaoming","xiaogang","xiaomei","xiaohong"]


The following code can be directly copied and used to check the effect. Finally, an operation to print the names variable needs to be added to view the results and compare Operation effect.
#print(names[0],names[-1],names[0:2],names[-2:-1],names[:3],names[-2:]) #respectively, print the first Segment, last segment, last 2 segments, the beginning segment is the data from segment 0 to segment 2 excluding xiaohong, and the data from the penultimate segment to the last segment (['xiaomei', 'xiaohong'])
#print (names.count("xiaoming")) #Print how many "xiaoming" there are in the variable
#print(names.index("xiaoming")) #Print the number of the value in the variable (the way of notation in the language: 0,1,2,3....)
#names.clear() #Clear all the values ​​in the variable
#names.insert(0,"xiaolizi") #Insert a piece of data, put the original "in the first place" xiaoming" squeezed to the second place
#names2=["daming","damei"]
#names.extend(names2) #Extend the value in the names2 variable in the names variable, the result after expansion: ['xiaoming', 'xiaogang', 'xiaomei', 'xiaohong', 'daming', 'damei']
# names.pop(0) #delete the first segment
#names.remove("xiaoming") #delete a segment according to the field name
#del names[0] #delete the first field
#names.reverse() #flip, flip The result after: ['xiaohong', 'xiaomei', 'xiaogang', 'xiaoming']
#names.sort() #Sort, the priority is: special symbols - "numbers - "uppercase -" the results of the primary school test: ['#xiaoming', '1xiaomei', 'Xiaogang', 'xiaohong']
#names.append("xiaogangpao") #Add a segment to the last line
print(names[-2:]) #


print (names)

Guess you like

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