Python全栈之路----数据类型—字典
Posted 沫小妤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python全栈之路----数据类型—字典相关的知识,希望对你有一定的参考价值。
字典:可变,一种key-value的数据类型
info = { ‘stu1101‘ : ‘TengLan Wu‘ , ‘stu1102‘ : ‘LongZe Luola‘ , ‘stu1103‘ : ‘ XiaoZe Maliya‘ }
1.特点:key-value结构;key必须可hash(被hash值不变),且必须唯一、必须为不可变类型;无序的(因为无索引,通过key查询);查找速度快
2.基本操作:创建,添加,查找,修改,删除,清空
1 >>> info = { 2 ... ‘龙婷‘:[24,‘design‘,‘UI‘,2333], 3 ...‘shanshan‘:[25,‘PR‘,‘wild model‘,13445222] 4 ... } #创建,‘:’前为key,后为value,value可为列表 5 >>> info 6 {‘龙婷‘: [24, ‘design‘, ‘UI‘, 2333], ‘shanshan‘: [25, ‘PR‘, ‘wild model‘, 13445222]} 7 >>> info[‘龙婷‘] #查找时,字典名[key值],若不存在,报错 8 [24, ‘design‘, ‘UI‘, 2333] 9 >>> info[‘龙婷‘][1] = ‘设计部‘ #value为列表,修改和列表类似 10 >>> info 11 {‘龙婷‘: [24, ‘设计部‘, ‘UI‘, 2333], ‘shanshan‘: [25, ‘PR‘, ‘wild model‘, 13445222]} 12 >>> ‘shanshan‘ in info #判断‘shanshan‘是否在列表里面 13 True 14 >>> info.get(‘shanshan‘) #获取‘shanshan‘的字典值 15 [25, ‘PR‘, ‘wild model‘, 13445222] 16 >>> info[‘shanshan‘] #获取‘shanshan‘的字典值 17 [25, ‘PR‘, ‘wild model‘, 13445222] 18 >>> info[‘alex‘] #获取‘alex‘的字典值,如果不存在,就报错 19 Traceback (most recent call last): 20 File "<stdin>", line 1, in <module> 21 KeyError: ‘alex‘ 22 >>> print(info.get(‘alex‘)) #获取‘alex‘的字典值,不存在返回为空 23 None 24 >>> info.pop(‘shanshan‘) #删除‘shanshan‘及其对应的value值,返回‘shanshan‘对应的value值 25 [25, ‘PR‘, ‘wild model‘, 13445222] 26 >>> info 27 {‘龙婷‘: [24, ‘design‘, ‘UI‘, 2333]} 28 >>> info.popitem() #随机删除,因为字典本身是无须的 29 (‘龙婷‘, [24, ‘design‘, ‘UI‘, 2333]) 30 >>> info 31 {} 32 >>> info = {‘龙婷‘: [24, ‘design‘, ‘UI‘, 2333], ‘shanshan‘: [25, ‘PR‘, ‘wild model‘, 13445222]} 33 >>> info[‘name‘] = ‘alex‘ #添加 key 为‘name‘,对应value为‘alex‘ 34 >>> info 35 {‘龙婷‘: [24, ‘design‘, ‘UI‘, 2333], ‘shanshan‘: [25, ‘PR‘, ‘wild model‘, 13445222], ‘name‘: ‘alex‘} 36 >>> del info[‘name‘] #删除‘name‘及其对应的value 37 >>> info 38 {‘龙婷‘: [24, ‘design‘, ‘UI‘, 2333], ‘shanshan‘: [25, ‘PR‘, ‘wild model‘, 13445222]}
3.多级字典的嵌套
>>> catelog = { ‘A‘:{‘a‘:[‘1‘,‘5‘]},‘B‘:{‘b‘:[‘2‘,‘4‘]},‘C‘:{‘c‘:[‘3‘,‘6‘]}} #创建一个嵌套列表 >>> catelog[‘B‘][‘b‘][1] += ‘***‘ #修改 >>> catelog {‘A‘: {‘a‘: [‘1‘, ‘5‘]}, ‘B‘: {‘b‘: [‘2‘, ‘4***‘]}, ‘C‘: {‘c‘: [‘3‘, ‘6‘]}}
4.字典的用法:.keys() .values() .items() .update() .setdefault() .copy()->用法和列表完全一致
1 >>> info = {‘alex‘:[24,‘IT‘],‘rain‘:[24,‘HR‘],‘jack‘:22} 2 >>> info 3 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: 22} 4 >>> info.keys() #打印所有的key值 5 dict_keys([‘alex‘, ‘rain‘, ‘jack‘]) 6 >>> info.values() #打印所有的value值 7 dict_values([[24, ‘IT‘], [24, ‘HR‘], 22]) 8 >>> info.items() #将字典里的元素打印成小元组 9 dict_items([(‘alex‘, [24, ‘IT‘]), (‘rain‘, [24, ‘HR‘]), (‘jack‘, 22)]) 10 >>> info 11 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: 22} 12 >>> dic2 = {1:2,2:3,‘jack‘:[22,‘Jack Ma‘,‘Alibaba CEO‘]} 13 >>> info.update(dic2) #info和update合并,key值相同的被覆盖 14 >>> info 15 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: [22, ‘Jack Ma‘, ‘Alibaba CEO‘], 1: 2, 2: 3} 16 >>> info.setdefault(2,‘new 2‘) 17 3 18 >>> info 19 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: [22, ‘Jack Ma‘, ‘Alibaba CEO‘], 1: 2, 2: 3} 20 >>> info.setdefault(‘test‘,‘new 2‘) #有key就获取对应value值,没有就创建 21 ‘new 2‘ 22 >>> info 23 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: [22, ‘Jack Ma‘, ‘Alibaba CEO‘], 1: 2, 2: 3, ‘test‘: ‘new 2‘} 24 >>> info.fromkeys([‘A‘,‘B‘,‘C‘]) #生成字典,给key批量赋值 25 {‘A‘: None, ‘B‘: None, ‘C‘: None} 26 >>> info.fromkeys([‘A‘,‘B‘,‘C‘],‘alex‘) #生成字典,给key批量赋值 27 {‘A‘: ‘alex‘, ‘B‘: ‘alex‘, ‘C‘: ‘alex‘} 28 >>> info 29 {‘alex‘: [24, ‘IT‘], ‘rain‘: [24, ‘HR‘], ‘jack‘: [22, ‘Jack Ma‘, ‘Alibaba CEO‘], 1: 2, 2: 3, ‘test‘: ‘new 2‘}
5.字典循环
>>> for k in info: ... print(k) #打印key ... alex rain jack 1 2 test
>>> for k in info: ... print(k,info[k]) #打印key和对应的value ... alex [24, ‘IT‘] rain [24, ‘HR‘] jack [22, ‘Jack Ma‘, ‘Alibaba CEO‘] 1 2 2 3 test new 2
>>> for k,v in info.items(): ... print(k,v) #打印key和value,但是比较低效(字典本身查询速度很快,此种方法将字典转化成了列表再循环,浪费时间),不建议使用 ... alex [24, ‘IT‘] rain [24, ‘HR‘] jack [22, ‘Jack Ma‘, ‘Alibaba CEO‘] 1 2 2 3 test new 2
以上是关于Python全栈之路----数据类型—字典的主要内容,如果未能解决你的问题,请参考以下文章