python路4—字典、集合,,#!/usr/bin


#!/usr/bin/python3#字典---增删改查#穿件一个字典info>>> info = {...     ‘student01‘:‘TenLan Wu‘,...     ‘student02‘:‘LongZe Luola‘,...     ‘student03‘:‘XiaoZe Maliya‘... ... } #字典查询                         >>> info                {‘student03‘: ‘XiaoZe Maliya‘, ‘student01‘: ‘TenLan Wu‘, ‘student02‘: ‘LongZe Luola‘}>>> info[‘student01‘]        #查询关键字student01对应的值,key不存在报错‘TenLan Wu‘>>> info.get(‘student01‘)    #询关键字student01对应的值,key不存在返回None‘TenLan Wu‘>>> info.get(‘student05‘)>>> print (info.get(‘stu‘))None>>> info.values()            #查询字典所有的值dict_values([‘XiaoZe Maliya‘, ‘TenLan Wu‘, ‘LongZe Luola‘])>>> info.keys()                #查询字典所有的关键字dict_keys([‘student03‘, ‘student01‘, ‘student02‘])>>> ‘student01‘ in info        #字典info是否存在关键字student01True>>> ‘student05‘ in infoFalse#修改>>> info[‘student01‘] = ‘Tomas‘    #增加>>> info[‘student01‘]‘Tomas‘>>> info[‘student04‘] = ‘new student‘>>> info{‘student03‘: ‘XiaoZe Maliya‘, ‘student01‘: ‘Tomas‘, ‘student02‘: ‘LongZe Luola‘, ‘student04‘: ‘new student‘}>>> info.setdefault(‘student01‘,‘default_name‘)        #key(student01)存在,不做操作,不存在就添加‘Tomas‘>>> info.setdefault(‘student06‘,‘default_name‘)‘default_name‘>>> info{‘student06‘: ‘default_name‘, ‘student03‘: ‘XiaoZe Maliya‘, ‘student01‘: ‘Tomas‘, ‘student02‘: ‘LongZe Luola‘, ‘student04‘: ‘new student‘}>>> #删除>>> del info[‘student04‘]>>> info[‘student04‘]Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: ‘student04‘>>> info.pop(‘student03‘)‘XiaoZe Maliya‘>>> info[‘student03‘]Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: ‘student03‘>>> info.popitem()            #随机删除一组key,value(‘student01‘, ‘Tomas‘)>>> >>> print (info.get(‘stu‘))None##>>> info{‘student02‘: ‘LongZe Luola‘, ‘student04‘: ‘new student‘}>>> b = {‘student01‘:‘new_student1‘,‘student02‘:‘TangSheng‘}>>> info.update(b)        #用b去更新info,key相同则不该,key不同添加>>> info{‘student04‘: ‘new student‘, ‘student01‘: ‘new_student1‘, ‘student02‘: ‘TangSheng‘}>>> >>> >>> dic = dict.fromkeys([6,7,8],‘test‘)        #初始化一个字典,三个keyy(6,7,8),value位test,>>> dic{8: ‘test‘, 6: ‘test‘, 7: ‘test‘}>>> dic2 = dict.fromkeys([6,7,8],[1,{‘name‘:‘tangsheng‘},444])>>> dic2{8: [1, {‘name‘: ‘tangsheng‘}, 444], 6: [1, {‘name‘: ‘tangsheng‘}, 444], 7: [1, {‘name‘: ‘tangsheng‘}, 444]}>>> dic2[7][1][‘name‘]        #多级字典‘tangsheng‘>>> >>> info.items()    #字典转化成列表dict_items([(‘student04‘, ‘new student‘), (‘student01‘, ‘new_student1‘), (‘student02‘, ‘TangSheng‘)])>>> print (info.items())dict_items([(‘student04‘, ‘new student‘), (‘student01‘, ‘new_student1‘), (‘student02‘, ‘TangSheng‘)])>>> ##循环字典>>> for i in info :...     print (i)... student04student01student02>>> for i in info:...     print (i,info[i])... student04 new studentstudent01 new_student1student02 TangSheng>>> for k,v in info.items():...     print (k,v)... student04 new studentstudent01 new_student1student02 TangSheng>>> #多级字典>>> multi_dic = {...     ‘湖北省‘:{...         ‘武汉市‘:[‘武昌‘,‘汉口‘,‘汉阳‘],...         ‘荆州市‘:[‘监利‘,‘朱河‘,‘沙市‘],...         ‘宜昌‘:[‘三游动‘,‘三峡大坝‘,‘清江画廊‘]...     },...     ‘湖南省‘:{...         ‘长沙市‘:[‘橘子洲‘,‘爱晚亭‘,‘长沙晚报‘],...         ‘岳阳市‘:[‘岳阳楼‘,‘洞庭湖‘,‘沙丘‘],...         ‘株洲市‘:[‘炎帝广场‘,‘神龙公园‘,‘石峰公园‘]... ...     },...     ‘河南省‘:{...         ‘郑州市‘:[‘嵩山‘,‘博物院‘,‘二七纪念塔‘],...         ‘洛阳市‘:[‘龙门石窟‘,‘白马寺‘,‘丽景门‘],...         ‘开封市‘:[‘开封府‘,‘特塔公园‘,‘清明上河图‘]... ...     }... ... }>>> >>> >>> multi_dic{‘湖北省‘: {‘荆州市‘: [‘监利‘, ‘朱河‘, ‘沙市‘], ‘武汉市‘: [‘武昌‘, ‘汉口‘, ‘汉阳‘], ‘宜昌‘: [‘三游动‘, ‘三峡大坝‘, ‘清江画廊‘]}, ‘河南省‘: {‘洛阳市‘: [‘龙门石窟‘, ‘白马寺‘, ‘丽景门‘], ‘开封市‘: [‘开封府‘, ‘特塔公园‘, ‘清明上河图‘], ‘郑州市‘: [‘嵩山‘, ‘博物院‘, ‘二七纪念塔‘]}, ‘湖南省‘: {‘长沙市‘: [‘橘子洲‘, ‘爱晚亭‘, ‘长沙晚报‘], ‘株洲市‘: [‘炎帝广场‘, ‘神龙公园‘, ‘石峰公园‘], ‘岳阳市‘: [‘岳阳楼‘, ‘洞庭湖‘, ‘沙丘‘]}}>>> multi_dic[‘湖北省‘][‘武汉市‘][1]‘汉口‘>>> multi_dic[‘湖北省‘][‘武汉市‘][1] += ‘,广埠屯‘>>> multi_dic[‘湖北省‘][‘武汉市‘][1]‘汉口,广埠屯‘>>> multi_dic{‘湖北省‘: {‘荆州市‘: [‘监利‘, ‘朱河‘, ‘沙市‘], ‘武汉市‘: [‘武昌‘, ‘汉口,广埠屯‘, ‘汉阳‘], ‘宜昌‘: [‘三游动‘, ‘三峡大坝‘, ‘清江画廊‘]}, ‘河南省‘: {‘洛阳市‘: [‘龙门石窟‘, ‘白马寺‘, ‘丽景门‘], ‘开封市‘: [‘开封府‘, ‘特塔公园‘, ‘清明上河图‘],  ‘郑州市‘: [‘嵩山‘, ‘博物院‘, ‘二七纪念塔‘]},  ‘湖南省‘: {‘长沙市‘: [‘橘子洲‘, ‘爱晚亭‘, ‘长沙晚报‘], ‘株洲市‘: [‘炎帝广场‘, ‘神龙公园‘, ‘石峰公园‘], ‘岳阳市‘: [‘岳阳楼‘, ‘洞庭湖‘, ‘沙丘‘]}}>>> #集合set>>> list_1 = [1,2,3,4,6,7,6,8,7,9]>>> set_1 = set(list_1)            #列表转化成集合>>> set_1{1, 2, 3, 4, 6, 7, 8, 9}>>> set_2 = set([2,3,4,5,10,22,43,])    #创建集合set_2>>> set_1,set_2({1, 2, 3, 4, 6, 7, 8, 9}, {2, 3, 4, 5, 10, 43, 22})>>> set_1.intersection(set_2)            #交集{2, 3, 4}>>> set_1.union(set_2)                    #并集{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 43, 22}>>> set_1.difference(set_2)                #差集 ,in set_1 but  no in set_2{8, 1, 9, 6, 7}>>> set_3 = set([1,2,6])>>> set_3.issubset(set_1)                #set_3 是否set_1的子集True>>> set_1.issuperset(set_3)                #set_1 是否set-3 的父集True>>> set_1.symmetric_difference(set_2)    #对称差集,合并set_1和set_2,并去掉去掉相同的元素{1, 5, 6, 7, 8, 9, 10, 22, 43}>>> set_1.isdisjoint(set_2)                #set_1和 set_2有交集返回False,没有返回TrueFalse>>> set_1 & set_2         #交集{2, 3, 4}>>> set_1 | set_2         #并集{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 43, 22}>>> set_1 - set_2         #差集{8, 1, 9, 6, 7}>>> set_1 ^ set_2         #对称差集{1, 5, 6, 7, 8, 9, 10, 22, 43}#增>>> set_1.add(999)        #添加一个元素>>> set_1{1, 2, 3, 4, 6, 7, 8, 9, 999}>>> set_1.update([9999,99999,9999999])        #添加多个元素>>> set_1{1, 2, 3, 4, 6, 7, 8, 9, 999, 9999, 9999999, 99999}>>> set_1.remove(2)            #删除一个元素,如果该元素不存在,就报错>>> set_1.remove(1)>>> set_1.remove(0)Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: 0>>> len(set_1)                #集合的长度10>>> ‘2‘ in set_1           #元素’2‘是否在集合set_1中False>>> ‘22‘ not in set_1True>>> set_1.pop()            #随机删除集合中的一个元素3>>> set_1.discard (2)    #删除集合元素2,如果不存在do noting>>> set_1.discard (4)>>> set_1{6, 7, 8, 9, 999, 9999, 9999999, 99999}>>> set_1.discard (6)>>> set_1.discard (55)

python路4—字典、集合

评论关闭