字典特点:无序、键唯一

字典的创建

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:\python\python.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:\python\python.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:\python\python.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:\python\python.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:\python\python.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:\python\python.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:\python\python.exe D:/untitled/dir/for.py
{'age': 18, 'name': 'chenxi'}
('hobby', '阅读')

  删除这个字典

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
del dis3
print(dis3)

  测试

D:\python\python.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:\python\python.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:\python\python.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:\python\python.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

  

  

  

最新文章

  1. CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
  2. linux下实现在程序运行时的函数替换(热补丁)
  3. 在SQL里如何写条件逻辑?
  4. SQL AND &amp; OR 运算符
  5. HDU 3622 Bomb Game(二分+2SAT)
  6. 详解&lt;a&gt;标签
  7. HTTP请求中的Body构建——.NET客户端调用JAVA服务进行文件上传
  8. How to set China Azure Storage Connection String
  9. for循环数据节点
  10. oracle 自定义异常处理
  11. Java 集合系列(一)
  12. Linux根文件系统
  13. Golang channel 特性
  14. 轻量级的Web框架——Nancy
  15. 关闭应用程序(主程序)(WPF)
  16. 201621123018《Java程序设计》第8周学习报告
  17. 21天实战caffe笔记_第三天
  18. ftps加密服务器
  19. 【BZOJ2683】简单题 [分治][树状数组]
  20. Android测试:从零开始3—— Instrumented单元测试1

热门文章

  1. CSS学习(8)盒模型
  2. yii2时区语言设置
  3. concat merge
  4. 校准产品质量,把控出海航向,腾讯WeTest《2019中国移动游戏质量白皮书》正式开放预约
  5. 【知识学习】PHP实现批量替换字典后缀
  6. 【代码审计】MenInfo文件包含漏洞
  7. Python学习第二十八课——Django(urls)
  8. 牛客跨年AK场-小sum的假期安排
  9. 与英特尔分道扬镳,苹果的5G业务掉队了吗?
  10. C++11常用特性介绍——constexpr变量