>>> x = ['a','b','c','d','e'] >>> y = [1,2,3,4,5] >>> for i,j in zip(x,y): print(i,j) a 1 b 2 c 3 d 4 e 5 for i in sorted(sor):排序 for i in sorted(set(sor)): 排序去重 for i in reversed(range(len(lis))):倒序
查询格式为: list[起始位置:结束位置:步长] list特点为顾头不顾腚,包含开头不包含结尾 步长默认为正序,步长1;可修改 如:1,2,3...... 步长也可为负数,则查询结果为倒序 如:-1,-2,-3..... 正序时,起始和结束也为正序,起始在前结束在后 倒序时,起始和结束也需为倒序,起始在后结束在前 倒序+步长 lis1 = ['a','b','c','d','e','f'] >>> lis1[::-1] ['f', 'e', 'd', 'c', 'b', 'a'] >>> lis1[-1:0:-1] ['f', 'e', 'd', 'c', 'b'] >>> lis1[::2] ['a', 'c', 'e'] 添加元素 lis1.append("g") >>> lis1 ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 删除元素 >>> li = lis1.pop() >>> li 'g' >>> lis1 ['a', 'b', 'c', 'd', 'e', 'f'] 插入元素 >>> lis1.insert(0,"y") >>> lis1 ['y', 'a', 'b', 'c', 'd', 'e', 'f'] 出现次数+出现位置 >>> lis2 = ['a','b','a','a','b','c'] >>> lis2.count('a') 3 >>> lis2.count('b') 2 >>> lis2.index("b") 1 >>> lis2.index('b',2) 4 >>> lis2.index('c') 5 排序+倒序 >>> lis3 = [3,5,1,8,2,4,6,3] >>> lis3.sort() >>> lis3 [1, 2, 3, 3, 4, 5, 6, 8] >>> lis3.sort(reverse=-1) >>> lis3 [8, 6, 5, 4, 3, 3, 2, 1] 扩展列表 >>> lis3[len(lis3):]=["a","b","c"] >>> lis3 [8, 6, 5, 4, 3, 3, 2, 1, 'a', 'b', 'c'] >>> lis3.extend(['d','e','f']) >>> lis3 [8, 6, 5, 4, 3, 3, 2, 1, 'a', 'b', 'c', 'd', 'e', 'f'] 去重 >>> l1 = ['b','c','d','b','c','a','a'] >>> l2 = list(set(l1)) >>> l2 ['c', 'b', 'a', 'd'] >>> l2=list(set(l1)) >>> l2.sort(key=l1.index)#去重后顺序不变 >>> l2 ['b', 'c', 'd', 'a'] >>> l1 ['b', 'c', 'd', 'b', 'c', 'a', 'a'] >>> l2=list({}.fromkeys(l1).keys()) >>> l2 ['b', 'c', 'd', 'a'] >>> l1 ['b', 'c', 'd', 'b', 'c', 'a', 'a'] >>> l2=[] >>> for i in l1: if not i in l2: l2.append(i) >>> l2 ['b', 'c', 'd', 'a'] >>> l1 ['b', 'c', 'd', 'b', 'c', 'a', 'a'] >>> l2 = [] >>> [l2.append(i) for i in l1 if not i in l2] [None, None, None, None] >>> l2 ['b', 'c', 'd', 'a'] import copy person = ['name',['a',100]] p1 = copy.copy(person) p2 = person[:] p3 = list(person) 浅copy创建联合帐号 p1 = person[:] p2 = person[:] p1[0] = 'A' P2[0] = 'B' p[1][1] = 50 print(p1,p2) 元组turple 只有count 和 index