标签:xxx 存在 功能 als xxxxx end append ever app
这里介绍下列表的功能 #切片:列表[a:b],从下标为a开始,到下标为(b-1)的元素 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[1:6]) # # 结果 # [1, 2, 3, 4, 5] # name = [0,1,2,3,4,5,6,7,8,9] # print(name[3:]) # # 结果 # [3, 4, 5, 6, 7, 8, 9] # name = [0,1,2,3,4,5,6,7,8,9] # print(name[:4]) # # 结果 # [0, 1, 2, 3] #打印下标为等差数列的元素的内容print(变量名[a:b:2]),这个2就为步长 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[0::2]) # # 结果 # [0, 2, 4, 6, 8] #切片还可以不断的切分元素的值 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # print(name[2]) # 结果 # [‘ZhouYongBo‘, ‘WangXianZhi‘] # # print(name[2][0]) # 结果 # ZhouYongBo # # print(name[2][0][2]) # 结果 # o #========================================================================================= #修改:列表名[1] = xxxxx #name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # name[0] = "CuiXiWen" # print(name) # 结果 # [‘CuiXiWen‘, ‘CuiHongYan‘, [‘ZhouYongBo‘, ‘WangXianZhi‘]] # # name[2][0] = "1234" # print(name) # 结果 # [‘CuiXiWen‘, ‘CuiHongYan‘, [‘1234‘, ‘WangXianZhi‘]] # name[2][0][0] = ‘A‘ # print(name) # # #注意,这里就不能改了 #=========================================================================================== #插入,变量名.insert(a,"xxxxx"),在列表为a的位置插入元素的内容为"xxxxxx",变量名.append"xxxxxxxxxxx"),在列表的最后插入一个元素,也就是追加; #这里如果是插入到-b的位置,则最终的插入的位置是-b-1的位置 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # name.insert(1,"CuiWeiMing") # print(name) # 结果 # [‘CuiYueRong‘, ‘CuiWeiMing‘, ‘CuiHongYan‘, [‘ZhouYongBo‘, ‘WangXianZhi‘]] # # # name.append("Cuiqing") # print(name) # 结果 # [‘CuiYueRong‘, ‘CuiWeiMing‘, ‘CuiHongYan‘, [‘ZhouYongBo‘, ‘WangXianZhi‘], ‘Cuiqing‘] #============================================================================================= #删除,用remove,变量名.remove("xxxxxx"),这里的xxxxxx不是下标,而是元素的值 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # name.remove("CuiYueRong") # print(name) # 结果 # [‘CuiHongYan‘, [‘ZhouYongBo‘, ‘WangXianZhi‘]] #删除,用pop,变量名.pop("xxxxxx"),这里的xxxxxx是下标,不是元素的内容 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # name.pop(1) # print(name) # 结果 # [‘CuiYueRong‘, [‘ZhouYongBo‘, ‘WangXianZhi‘]] #删除列表,用del 列表名 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # del name # print(name) #del也可以删除多个连续的元素 del name[a:b],删除下标为a的元素到下标为(b-1)的元素 # name = ["CuiYueRong","CuiHongYan",["ZhouYongBo","WangXianZhi"]] # print(name) # del name[1:] # print(name) # # 结果 # [‘CuiYueRong‘] #================================================================================ #获取列表中某个元素的下标,如果列表中有多个这个元素,只会取第一个元素的下标 # num = [1,2,3,4,4,5,3,4] # print(num.index(1)) # print(num.index(4)) # # 结果 # 0 # 3 #================================================================================== #获取某个元素在列表中出现的次数 列表名.count("xxxx"),统计列表中xxxx出现的次数 # num = [1,2,3,4,4,5,3,4] # print(num.count(4)) # # 结果 # 3 # num = [1,2,3,4,4,5,3,4] # print(num.count(0)) # 结果 # 0 #=================================================================================== #检查一个列表中是否存在某个值,用in的方法 # num = [1,2,3,4,4,5,3,4] # print(2 in num) # 结果 # True # num = [1,2,3,4,4,5,3,4] # print("a" in num) # 结果 # False # num = [1,2,3,4,4,5,3,4] # if "a" in num: # print("a is in num") # else: # print("a is not in num") # # num = [1,2,3,4,4,5,3,4] # if 2 in num: # print("2 is in num") # else: # print("2 is not in num") #========================================================================== #copy一个列表,这里在python2.6中需要导入一个模块才能支持,这里需要解释一下 # import copy,这里copy,如果拷贝的list中还有一个列表,则子列表不会拷贝过来,此时如果修改子列表的内容,则该子列表的母列表和新拷贝出来的 #列表的值都会被改变 # num = [0,1,2,3,4,["a","b","c","d"]] # num1 = copy.copy(num) # print(num1) # # num[5][0] = "A" # num[0] = "Cuiyuerong" # print(num,num1) # 结果 # ([‘Cuiyuerong‘, 1, 2, 3, 4, [‘A‘, ‘b‘, ‘c‘, ‘d‘]], [0, 1, 2, 3, 4, [‘A‘, ‘b‘, ‘c‘, ‘d‘]]) # import copy,这里描述一下deepcopy,会把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) # 结果 # ([‘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) #======================================================================== #反转列表:列表名.reverse() # num1 = [0,1,2,3,4,["a","b","c","d"]] # # num1.reverse() # print(num1) # # 结果 # [[‘a‘, ‘b‘, ‘c‘, ‘d‘], 4, 3, 2, 1, 0 #======================================================================== #对列表重新排序,列表名.sort() # num1 = ["d",1,2,3,0,["a","b","c","d"]] # num1.sort() # print(num1) # # 结果 # [0, 1, 2, 3, [‘a‘, ‘b‘, ‘c‘, ‘d‘], ‘d‘] #=========================================================================== #来一个小练习,如果一个list中有多个相同的元素,现在的需求是把list中的这个元素所有的值都修改为另外的一个值 # 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)标签:xxx 存在 功能 als xxxxx end append ever app
原文地址:http://www.cnblogs.com/bainianminguo/p/6403958.html