博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
list,tuple,set,dict基础
阅读量:7105 次
发布时间:2019-06-28

本文共 4633 字,大约阅读时间需要 15 分钟。

list

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] = 0110 print(L1,L2)111 '''112 '''113 import copy114 L1 = [1,2,3]115 L2 = copy.deepcopy(L1)#copy 一份独立的列表116 L2[1] = 0117 print(L1,L2)118 '''119 '''120 L1 = [1,2,3]121 L2 = L1122 L2[1] = 0123 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 add141 L1 = L.insert(1,'0')142 L2 = L.append('g')143 #modify144 L3 = L[0] = '1'145 #delete146 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 '''
View Code

 

tuple

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_tuple212 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'
View Code

 

 

set

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 #添加
View Code

 

string

str1 = 'good good study,day day up' print(str1.capitalize())  # 首字母大写 print(str1.center(50,'-')) list1 = ['1','2','3'] print('+'.join(list1)) 

dict

转载于:https://www.cnblogs.com/ray-mmss/p/9376557.html

你可能感兴趣的文章
驱动应该怎么学习?
查看>>
I.MX6 AW-NB177NF wifi HAL 调试修改
查看>>
Linux 双网卡双网段通信
查看>>
I.MX6 Linux U-boot 环境变量解析
查看>>
rtems源码补丁贡献要求(官网解析)
查看>>
Linux如何从零开始搭建nfs服务器(centOS6)
查看>>
固定宽度布局的列背景色设置
查看>>
dragsort html拖拽排序
查看>>
System.out.println(request.getContextPath());
查看>>
非常正经的看毛片(?)
查看>>
CF 514C(hash)
查看>>
工程代码の初體驗
查看>>
JS节点操作 (表格在js中添加行和单元格,并有一个删除键)
查看>>
poj 1338
查看>>
关于c++中map的内存占用问题
查看>>
SoftEng-logo队成立了!
查看>>
REPLACE...IN.....WITH.... 的使用
查看>>
static变量和static函数
查看>>
linux gcc编译protocol
查看>>
让linux启动后自动进入图形界面或文本界面
查看>>