Here is a list of features of the following table: [A:B], starting from subscript A, to subscript (b-1) Element # name = [0,1,2,3,4,5,6,7,8,9]# print (Name[1:6]) # # result # [1, 2, 3, 4, 5]# name = [ 0,1,2,3,4,5,6,7,8,9]# print (name[3:]) # # results # [3, 4, 5, 6, 7, 8, 9]# name = [0,1,2,3,4,5,6,7,8,9]# print (Name[:4]) # # results # [0, 1, 2, 3] #打印下标为等差数列的元素的内容print (variable name [A:b:2]), this 2 is the step # name = [0,1,2,3,4,5,6,7,8,9]# print (Name[0::2]) # # results # [0, 2, 4, 6, 8]# Slices can also continuously slice the value of the element # name = ["Cuiyuerong", "Cuihongyan", ["Zhouyongbo", "Wangxianzhi"]]# print (name[2]) # result # [' Zhouyongbo ', ' Wangxianzhi ']## print (name[2][0]) # result # zhouyongbo## print (name[2][0][2]) # results # o#==================================== ===================================================== #修改: List name [1] = Xxxxx#name = ["Cuiyuerong", "Cuihongyan", [" Zhouyongbo "," Wangxianzhi "]]# name[0] =" Cuixiwen "# print (name) # result # [' Cuixiwen ', ' Cuihongyan ', [' Zhouyongbo ', ' Wangxianzhi ']]## name[2][0] = "1234" # print (name) # result # [' Cuixiwen ', ' Cuihongyan ', [' 1234 ', ' Wangxianzhi ']]# name[2][0][0 ] = ' A ' # print (name) # # # #注意, you can't change it here #=========================================================================================== #插入, variable name. Insert (A, "xxxxx"), inserts the contents of the element at the position of a in the list as " XXXXXX ", variable name. Append" Xxxxxxxxxxx "), insert an element at the end of the list, i.e. append, #这里如果是插入到-B position, then the final insertion position is the location of the-b-1 # name = [" Cuiyuerong "," Cuihongyan ", [" Zhouyongbo "," Wangxianzhi "]]# name.insert (1," cuiweiming ") # print (name) # # result # [' Cuiyuerong ', ' cuiweiming ', ' Cuihongyan ', [' Zhouyongbo ', ' Wangxianzhi ']]### name.append ("cuiqing") # print (name) # result # [' Cuiyuerong ', ' cuiweiming ', ' Cuihongyan ', [' Zhouyongbo ', ' Wangxianzhi '], ' cuiqing ']#======================================================= ====================================== #删除, with remove, variable name. Remove ("xxxxxx"), where the xxxxxx is not subscript, but the value of the element # name = ["Cuiyuerong" , "Cuihongyan", ["Zhouyongbo", "Wangxianzhi"]]# name.remove ("Cuiyuerong") # print (name) # result # [' Cuihongyan ', [' Zhouyongbo ', ' Wangxianzhi ']] #删除, with pop, variable name. Pop ("xxxxxx"), where the xxxxxx is subscript, not the content of the element # name = ["Cuiyuerong", "Cuihongyan", [" Zhouyongbo "," Wangxianzhi "]]# Name.pop (1) # print (name) # result # [' Cuiyuerong ', [' Zhouyongbo ', ' Wangxianzhi ']] #删Except for the list, with the Del list name # NAME = ["Cuiyuerong", "Cuihongyan", ["Zhouyongbo", "Wangxianzhi"]]# del name# print (name) #del也可以删除多个连续的元素 Del Name[a:b], remove the element labeled A to the subscript (b-1) Element # name = ["Cuiyuerong", "Cuihongyan", ["Zhouyongbo", "Wangxianzhi"]]# print (name) # Del name[1:]# Print (name) # # result # [' Cuiyuerong ']#=================================================================== ============= #获取列表中某个元素的下标, if there is more than one element in the list, only the subscript # of the first element is taken, num = [1,2,3,4,4,5,3,4]# print (Num.index (1)) # Print (Num.index ( 4) # # results # 0# 3#================================================================================== #获取某个元素在列表中出现的次数 List name. Count ("xxxx"), count the number of occurrences of XXXX in the list # num = [1,2,3,4,4,5,3,4]# print (Num.count (4)) # # result # 3# num = [1,2,3,4,4,5,3,4]# print ( Num.count (0)) # results # 0#===================================================================================# Check if a value exists in a list with in method # num = [1,2,3,4,4,5,3,4]# print (2 in num) # result # true# num = [1,2,3,4,4,5,3,4]# print ("a" in num) # result # false# num = [1,2,3,4,4,5,3,4]# if "a" in num:# print ("A was in num") # else:# PRint ("A is not in Num") # # num = [1,2,3,4,4,5,3,4]# if 2 in num:# print ("2 are in num") # else:# Print ("2 are not in num") #=== ======================================================================= #copy一个列表, Here in python2.6 need to import a module in order to support, here to explain the # import copy, here Copy, if there is a list in the copy, the sub-list will not be copied, if you modify the contents of the sub-list, then the parent list of the child list and the new copy of the #列表的值都会被改变 # num = [0,1,2,3,4,["A", "B", "C", "D"]]# NUM1 = copy.copy (num) # print (NUM1) # # Num[5][0] = "A" # Num[0] = "Cuiyueron G "# Print (NUM,NUM1) # results # ([' Cuiyuerong ', 1, 2, 3, 4, [' A ', ' B ', ' C ', ' d ']], [0, 1, 2, 3, 4, [' A ', ' B ', ' C ', ' d ']] # import Copy, which describes deepcopy, copies the contents of all the lists in the list. num = [0,1,2,3,4,["A", "B", "C", "D"]]# num2 = copy.deepcopy (num) # Num[5][0] = "A" # Num[0] = "Cuiyuerong" # print (num,num2) # results # ([' Cuiyuerong ', 1, 2, 3, 4, [' A ', ' B ', ' C ', ' d ']], [0, 1, 2, 3, 4, [' A ', ' B ', ' C ', ' d ']]) #========================================================================= #用extend把两个列表合在一起 # NUM1 = [0,1,2,3,4,["A", "B", "C", "D"]]# num2 = [0,1,2,3,4,["A", "B", "C", "D"]]# Num1.extend (num2) # print (NUM1) #======================================================================== #反转列表: List name. reverse () # NUM1 = [0,1,2,3,4,["A", "B", "C", "D"]]## Num1.reverse () # Print (NUM1) # # results # [[' A ', ' B ', ' C ', ' d '], 4, 3, 2, 1, 0#============ ============================================================ #对列表重新排序, list name. Sort () # NUM1 = ["D", 1,2,3,0,["a", "B", "C" , "D"]]# Num1.sort () # Print (NUM1) # # results # [0, 1, 2, 3, [' A ', ' B ', ' C ', ' d '], ' d ']#=========================================== ================================ #来一个小练习, if there are multiple identical elements in a list, the requirement now is to modify all the values of this element in the list to another value # num = [ 1,2,3,4,5,6,1,1,6,77,1,2,45,1,3,1]# Count_of_ele = Num.count (1) # for I in Range (Count_of_ele): # Index_of_ele = Num.index (1) # Num[index_of_ele] = "A" # Print (Num.count (1)) # Print (Num.count ("a")) # print (num)
List of Python lists