字典特点:无序、键唯一
字典的创建
bag = {'cx':'chenxi','gghg':35}
print(bag['cx'])
测试
chenxi Process finished with exit code 0
字典操作之增加
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['zd']='zrd'
print(cx)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'chenxi', 'user': 'haha', 'zd': 'zrd'}
字典操作之修改值操作
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
字典操作之新增操作
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
测试
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
查看字典里所有的键
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(cx.keys()) #查看字典里所有的键
测试
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
dict_keys(['cx', 'user', 'df'])
查看字典中所有的键;并转换成列表数据类型
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(list(cx.keys())) #查看字典里所有的键;并把它转换成列表数据结构
测试
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
['cx', 'user', 'df']
查看字典中所有的值,并以列表方式显示
cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(list(cx.keys())) #查看字典里所有的键;并把它转换成列表数据结构
print(list(cx.values())) #查看字典所有值,并转换成列表数据结构
测试
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
['cx', 'user', 'df']
['dne', 'haha', 78]
修改字典里键的值
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
print(dis3)
dis3['age']=55 #age的值改55
print(dis3)
测试
{'age': 18, 'name': 'chenxi', 'hobby': '阅读'}
{'age': 55, 'name': 'chenxi', 'hobby': '阅读'}
Process finished with exit code 0
更新
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
print(dis3)
cx7={'sdf':'csd','ga':'gffg','yu':'ggh'}
print(cx7)
dis3.update(cx7)
print(dis3)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'age': 18, 'name': 'chenxi', 'hobby': '阅读'}
{'sdf': 'csd', 'ga': 'gffg', 'yu': 'ggh'}
{'age': 18, 'name': 'chenxi', 'hobby': '阅读', 'sdf': 'csd', 'ga': 'gffg', 'yu': 'ggh'}
Process finished with exit code 0
删
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
del dis3['age']
print(dis3)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'name': 'chenxi', 'hobby': '阅读'}
Process finished with exit code 0
清空字典操作
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
dis3.clear()
print(dis3)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{}
Process finished with exit code 0
删除字典中某键值并把所删的值重新打印出来
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
ret = dis3.pop('age')
print(dis3)
print(ret)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'name': 'chenxi', 'hobby': '阅读'}
18
Process finished with exit code 0
随机删除一对键值,并把删除的这对键值打印出来
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
ret = dis3.popitem()
print(dis3)
print(ret)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'age': 18, 'name': 'chenxi'}
('hobby', '阅读')
删除这个字典
dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
del dis3
print(dis3)
测试
D:pythonpython.exe D:/untitled/dir/for.py
Traceback (most recent call last):
File "D:/untitled/dir/for.py", line 126, in <module>
print(dis3)
NameError: name 'dis3' is not defined
创建值相同的字典
dic6 = dict.fromkeys(['cx-1','cx-2','cx-3'],'test') print(dic6)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'cx-1': 'test', 'cx-2': 'test', 'cx-3': 'test'}
Process finished with exit code 0
注意
dic6 = dict.fromkeys(['cx-1','cx-2','cx-3'],['test-1','test-2']) print(dic6) dic6['cx-2'][1]='abc' print(dic6)
测试
{'cx-1': ['test-1', 'test-2'], 'cx-2': ['test-1', 'test-2'], 'cx-3': ['test-1', 'test-2']}
{'cx-1': ['test-1', 'abc'], 'cx-2': ['test-1', 'abc'], 'cx-3': ['test-1', 'abc']}
嵌套字典修改
av_cte = {
"中国":{
"河北":["不错","历史"],
"广州":["喜欢","沿海"],
"长沙":["适合玩"],
"北京":["房价死贵"]
},
"消费" :{
"河北":["一般"],
"广州":["还好"],
"上海":["没去过"],
"长沙":["没去过"],
"北京":["小贵"]
}
}
print(av_cte)
av_cte['中国']['广州'][1]="hhh"
print(av_cte)
测试
D:pythonpython.exe D:/untitled/dir/for.py
{'中国': {'河北': ['不错', '历史'], '广州': ['喜欢', '沿海'], '长沙': ['适合玩'], '北京': ['房价死贵']}, '消费': {'河北': ['一般'], '广州': ['还好'], '上海': ['没去过'], '长沙': ['没去过'], '北京': ['小贵']}}
{'中国': {'河北': ['不错', '历史'], '广州': ['喜欢', 'hhh'], '长沙': ['适合玩'], '北京': ['房价死贵']}, '消费': {'河北': ['一般'], '广州': ['还好'], '上海': ['没去过'], '长沙': ['没去过'], '北京': ['小贵']}}
字典排序
dic = {5:'888',8:'44544',3:'895'}
print(dic)#未排序的
print(sorted(dic))# 键排序
print(sorted(dic.values())) #按键值排序
print(sorted(dic.items())) # 按键排序
测试
D:pythonpython.exe D:/untitled/dir/for.py
{5: '888', 8: '44544', 3: '895'}
[3, 5, 8]
['44544', '888', '895']
[(3, '895'), (5, '888'), (8, '44544')]
Process finished with exit code 0
字典遍历;效率高
dic = {5:'888',8:'44544',3:'895'}
for i in dic:
print(i,dic[i])
测试
5 888 8 44544 3 895
字典遍历之2
dic = {5:'888',8:'44544',3:'895'}
for i,v in dic.items():
print(i ,v)
测试
5 888 8 44544 3 895