1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 __author__ = 'Howie' 4 names = ['ZhangSan', 'LiSi', 'WangWu',['lili','dddd'], 'ZhaoLiu','DAda'] 5 #A = names[-2:] #如果想取倒数几个数需要吧最后一个省略 6 A = names[:3] #如果想取第一个开始到第几个数可以把索引值省略 7 print(A) 8 names.append('Lili')#追加 9 print(names) 10 name2 = names.copy() 11 print('浅拷贝后:',name2) 12 names[2] = '王五' 13 names[3][0] = 'LILI' 14 print('原列表:',names) 15 print('浅拷贝后再修改原列表的子列表:的',name2) 16 17 #如果想要完全复制的话要导入一个模块 18 import copy 19 name2 = copy.copy(names) 20 print('原列表:',names) 21 print('完全复制后:', name2) 22 23 24 25 #names.pop(1) #删除第二个 26 #del names[1] #和上面的差不多一样的 27 names.remove('LiSi') #匹配删除 28 print('删除',names) 29 names.insert(1,'DAda')#中间插入数值问索引值 30 print(names) 31 names[2] = 'xiugai' # 改就是等再赋值一次 32 print(names) 33 print('查询值在索引位置:',names.index('DAda')) 34 print('统计名字次数:',names.count('DAda')) 35 names.reverse() 36 names2 = ['66','3','6','8','1','5'] 37 names.extend(names2) 38 print('扩张:',names) 39 40 #names.sort() 41 #print('排序:',names) 42 43 44 45 46 names.clear() 47 print(names)
1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 __author__ = 'Howie' 4 list_1 = [1,5,8,6,2,4,3,2,5,1,1,6] 5 list_2 = set([1,5,5,1,1,6,44,11,22]) 6 list_1 = set(list_1) 7 print(list_1,list_2) 8 print('交集还可以用&来做:',list_1.intersection(list_2)) 9 print('交集还可以用&来做:',list_1 &list_2) 10 print('并集:',list_1.union(list_2)) 11 print('并集还可以用|来做:',list_1|list_2) 12 print('差集:',list_1.difference(list_2))#1有但是2没有的 13 print('差集还可以用-来做:',list_1-list_2)#1有但是2没有的 14 print('对称差集:',list_1.symmetric_difference(list_2))#除了交集以外的集合 15 print('对称差集还可以用^来做:',list_1^list_2)#除了交集以外的集合 16 17 list_3 = list_1.union(list_2) 18 print('子集:',list_1.issubset(list_2),list_1.issubset(list_3))#判断是不是子集 19 print('父集:',list_3.issuperset(list_1)) 20 list_4 = set([333,5555,66666]) 21 print('判断是不是没有交集如果没有就为True:',list_1.isdisjoint(list_4)) 22 list_4.add(44444) 23 list_4.update([11111,2222,66,333]) 24 list_4.remove(333) 25 print(list_4)
1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 __author__ = 'Howie' 4 info = { 5 'zhangsan':500, 6 'lisi':600, 7 'zhaowu':700 8 } 9 b = { 10 'lisi':789, 11 1:2, 12 3:4 13 } 14 ''' 15 print(type(info)) 16 print(info) 17 print(info['lisi']) 18 info['lisi'] = '李四' 19 print(info) 20 info['wangwu'] = '王五' 21 print(info) 22 del info['zhaowu'] 23 print(info) 24 print(info.get('lili')) 25 print('lili' in info) 26 27 info.setdefault('lisi',{456}) 28 print(info) 29 30 info.update(b) 31 ''' 32 A = info.items() 33 A = info.fromkeys([7,8,9]) 34 35 print(A)