#!/usr/local/bin/python3 # -*- coding:utf-8 -*- names=['zhangyu','mahongyan','zhangguobin','shachunhua'] #-----increase----- '''names.append('zhangzhongjian') #Insert an element at the end of the list names.insert(1,'aaa') #Insert an element ''' at the specified position in the list #-----delete----- '''names.remove('zhangyu') #Remove the element named 'zhangyu' in the list del names[1] #Delete the element at the specified position in the list names.pop() #Delete the last element in the list by default names.pop(0) #Delete the element ''' at the specified position in the list #-----change----- #names[2]='bbb' #-----check----- '''print(names[0],names[2]) print(names[0:3]) #Ignore the head and ignore the tail/slicing print(names[-1]) #take the last one print(names[-2:]) #Take the last two print(names[:3]) #Take the first two ''' #-----Find----- '''print(names.index('zhangyu')) #Find the subscript (position) of 'zhangyu' in the list print(names[names.index('zhangyu')]) #Retrieve the corresponding value in the list according to the subscript''' #-----statistics----- #print(names.count('zhangyu')) #Count the number of times 'zhangyu' appears in the list #-----Clear----- #names.clear() #Clear the elements in the list #-----Reverse----- #names.reverse() #Reverse the entire list #-----Sort----- #names.sort() # Sort the list, the priority is: special symbols --> numbers --> uppercase letters --> lowercase letters #-----Merge list----- '''names2=[1,2,3,4] #Merge names2 into names names.extend(names2) del names2 #delete names2 print(names)''' #-----copy----- '''names1=['zhangyu','mahongyan',['aaa','bbb'],'zhangguobin','shachunhua'] names2=names1.copy() print(names2,names1) names1[0]='Zhang Yu' names1[2][0]='abc' print(names2,names1) #It can be seen that when the elements in names1 change, names2 does not completely copy names1 (shallow copy) #So how to perform deep copy? -->Introduce copy module import copy names1=['zhangyu','mahongyan',['aaa','bbb'],'zhangguobin','shachunhua'] names2=copy.deepcopy(names1) #深copy print(names2,names1) names1[0]='Zhang Yu' names1[2][0]='abc' print(names2,names1) #At this time, no matter how the elements in names1 change, the elements in names2 are still the elements in the original names1''' #-----cycle----- '''for x in names: print(x)''' #-----step slice----- #print(names[0:-1:2])