list
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # @Auther : chen 2 # @Time : 2018/4/26 19:55 3 # @File : list_ex.py 4 # @SoftWare : PyCharm 5 6 # list1 = [1,2,3,4,5,6,7,8,9,0] 7 # random.shuffle(list1) 8 # print(list1) 9 # for i in list1: 10 # print(i) 11 12 name_list = ['alex','bill','chris','eve','frank','ray','suker'] 13 name_iter=iter(name_list) 14 print(name_iter.__next__()) 15 # print(name_list.index('chris')) 16 # name_list.insert(4,'jack')#指定位置插入 17 # print(name_list) 18 # name_list.reverse()#list 反转 19 # print(name_list) 20 # name_list.remove('eve')#删除指定的值 21 # print(name_list) 22 # name_list.pop()#删除一个列表尾部数据 23 # name_list.sort()#按顺序排列原list 24 # chg_list = sorted(name_list)#排序 25 # name_list.append('alex')#尾部追加 26 # print(name_list) 27 #删除列表中指定的值 28 # for val in range(name_list.count('alex')): 29 # name_list.remove('alex') 30 # print(name_list) 31 #切片 顾头不顾尾 32 # list_src = [1,2,3,4,5,6,7,8,9,0] 33 # print(list_src[0:3]) 34 # print(list_src[:3]) 35 # print(list_src[-3:-1]) 36 # print(list_src[-3:]) 37 # print(list_src[0:9:3]) 38 #列表拼接 39 # name_list = name_list + list_src 40 # print(name_list) 41 # list_src.extend(name_list) 42 # print(list_src) 43 # list1 = [0,1,2,3,4,5,6,7,8,9,0] 44 # list2 = 'ray chen' 45 # list1.extend(list2) 46 # print(list1) 47 # list3 = [] 48 # list3.extend(list2) 49 # print(list3) 50 #元组和列表互转 51 list1 = ['alex','suker','flex'] 52 print(list1.count('alex')) 53 tuple1 = tuple(list1) 54 print(list1,tuple1) 55 list2 = list(tuple1) 56 print(list2,tuple1) 57 58 ''' 59 import copy 60 list1 = [1,2,3,4,5] 61 list2 = ['a','b','c','d'] 62 for ele in zip(list1,list2): 63 print(ele) 64 p1 = ['name',['age',170],'male'] 65 p2 = list(p1) 66 p2[0] = 'name2' 67 p2[1][1] = 175 68 print('--result2--') 69 print(p1,p2) 70 p3 = copy.copy(p1) 71 p3[0] = 'name3' 72 p3[1][1] = 180 73 print('--result3--') 74 print(p1,p3) 75 p4 = p1[:] 76 p4[0] = 'name4' 77 p4[1][1] = 185 78 print('--result4--') 79 print(p1,p3) 80 p5 = p1 81 p5[0] = 'name5' 82 p5[1][1] = 190 83 print('--result5--') 84 print(p1,p5) 85 p6 = copy.deepcopy(p1) 86 p6[0] = 'name6' 87 p6[1][1] = 195 88 print('--result6--') 89 print(p1,p6) 90 ''' 91 ''' 92 #浅COPY 93 p1 = ['name',['age',170],'male'] 94 p2 = list(p1) 95 p2 = copy.copy(p1) 96 p2 = p1[:] 97 p2 = p1 98 99 ''' 100 ''' 101 L1 = ['A','B','C',['D','E','F'],'G'] 102 L2 = L1.copy() 103 L2[3][2] = 'F1' 104 print(L1,L2) 105 ''' 106 ''' 107 L1 = [1,2,3] 108 L2 = L1.copy() 109 L2[1] = 0 110 print(L1,L2) 111 ''' 112 ''' 113 import copy 114 L1 = [1,2,3] 115 L2 = copy.deepcopy(L1)#copy 一份独立的列表 116 L2[1] = 0 117 print(L1,L2) 118 ''' 119 ''' 120 L1 = [1,2,3] 121 L2 = L1 122 L2[1] = 0 123 print(L1,L2) 124 ''' 125 ''' 126 L1 = ['A','B','C',['D','E','F'],'G'] 127 L2 = L1.copy() 128 print(L1,L2) 129 L1[2] = 'C1' 130 print(L1,L2) 131 L1[3][2] = 'F1' 132 print(L1,L2) 133 L2[2] = 'B1' 134 print(L1,L2) 135 L2[3][1] = 'D1' 136 print(L1,L2) 137 ''' 138 ''' 139 L = ['a','b','c','d','e','f'] 140 # insert add 141 L1 = L.insert(1,'0') 142 L2 = L.append('g') 143 #modify 144 L3 = L[0] = '1' 145 #delete 146 L4 = L.remove('g') 147 L5 = L.pop() 148 del L[0] 149 #切片 150 print(L) 151 print(L[:5]) #取前5个 == print(L[0:5]) 152 print(L[1:5]) #从下标1取到下标4(顾头不顾尾) 153 print(L[4]) #取第4个元素 154 print(L[-1])#取最后一个元素 155 print(L[-3:])#取最后3个元素 156 print(L[-3:-1])# 取倒数第二和第三个元素 157 print(L.count('e')) 158 L.reverse() #反转 159 L.sort()#排序 160 sorted(L)#排序 161 L.index('e')#打印元素索引 162 L.extend(L5) #拼接2个列表 163 print(L) 164 L6 = L.copy()#复制列表 165 L.clear()#清空 166 del L #删除列表 167 '''
tuple
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #tuple比list好的地方 2 #1.性能优化(immutable会作为常量在编译时确定)(immutable--不可变) 3 #2.线程安全 4 #3.可以作为dict的key 5 #4.可拆包 6 #tuple 拆包 7 user_tuple1 = ('ray',27,170) 8 name,age,height = user_tuple1 9 print(name,age,height) 10 user_tuple2 = ('ray',27,170,'shiyan','college') 11 name,*other = user_tuple2 12 print(name,other) 13 #tuple不可变性不是绝对的 14 user_tuple3 = ('ray',[27,'shiyan']) 15 print(user_tuple3) 16 user_tuple3[1].append('college') 17 print(user_tuple3) 18 user_dict = {} 19 user_dict[user_tuple1] = 'ray'
set
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #list 转 set 2 list_1 = [1,3,5,7,9,11,13] 3 list_1 = set(list_1) 4 list_2 = {3,7,9,11,2,4,6} 5 #交集 6 print(list_1.intersection(list_2)) 7 print(list_1 & list_2) 8 #并集 9 print(list_1.union(list_2)) 10 print(list_1 | list_2) 11 #差集 12 print(list_1.difference(list_2)) 13 print(list_1 - list_2) 14 #子集/父集 15 print(list_1.issubset(list_2)) 16 print(list_1.issuperset(list_2)) 17 #对称差集(两个集合中互相不存在的) 18 print(list_1.symmetric_difference(list_2)) 19 print(list_1 ^ list_2) 20 #添加
string
str1 = 'good good study,day day up'
print(str1.capitalize()) # 首字母大写
print(str1.center(50,'-'))
list1 = ['1','2','3']
print('+'.join(list1))
dict