字典的基本方法

什么是字典:

字典是一种 key - value的数据类型,听alex说就像我们上学用的字典,通过笔划,字母来查找对饮页面的详细内容。

语法:

id_dict = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}

字典的特性:

  dict是无序的

  key必须是唯一的,value可以重复,    key=键,value=值

增加:

id_dict["stu1104"] = "smelond"
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'smelond'}

修改:

id_dict["stu1101"] = "amanda"
print(id_dict)
{'stu1101': 'amanda', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

删除里面的某一项:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.pop("stu1101")#标准删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

del删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} del id_dict["stu1101"]#del删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

随机删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.popitem()#随机删除
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola'}

直接删字典:

print(id_dict)
id_dict.clear() #删除字典里面的所有内容
print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
{}

查找:

print("stu1101" in id_dict)#in查看有没有这个对象
True #如果有返回真,没有则fFalse

获取:

print(id_dict.get("stu1101"))#用get获取如果存在返回key值,不存在则返回None,
TengLan Wu
print(id_dict["stu1101"])#这个方法不会像上面那样智能,key不存在就直接报错
TengLan Wu
print(id_dict["stu11231"])
print(id_dict["stu11231"])
KeyError: 'stu11231'

多字典嵌套:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} print(id_dict["ChengDu"])#打印输出所有的值
print(id_dict["ChengDu"]["acreage"]) #打印输出成都的面积
id_dict["ChengDu"]["acreage"] = "面 积:14312平方千米" #给成都的面积重新修改为了"面 积:14312平方千米"
print(id_dict["ChengDu"]["acreage"])#打印 {'acreage': 14312, 'population': '1591.8w', 'postalcode': 610000}
14312
面 积:14312平方千米

将字典转换为元组:

print(id_dict.items())
dict_items([('stu1101', 'TengLan Wu'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya')])

将字典转换为列表:

list_test = list(id_dict)
print(list_test) ['stu1101', 'stu1102', 'stu1103']

字典的循环:

for key in id_dict:
print(key, id_dict[key])#由于加入了key,所以他把stu也循环出来了
输出:
stu1101 TengLan Wu
stu1102 LongZe Luola
stu1103 XiaoZe Maliya for key in id_dict:
print(id_dict[key])
输出:
TengLan Wu
LongZe Luola
XiaoZe Maliya

多字典嵌套的循环:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} for key in id_dict:#还是照常循环
test = id_dict[key]#将我们每次的key赋给test
for key in test:#再来循环test
# print(key, ":", test[key], "\n", end="")
print("%s : %s" % (key, test[key]))#将第二级的key打印,并且打印出value acreage : 14312
population : 1591.8w
postalcode : 610000
acreage : 1196
population : 1190.08w
postalcode : 518000
acreage : 1.641w
population : 2172.9w
postalcode : 100000

最新文章

  1. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
  2. 解决:tomcat部署时deploy location不能显示加载后的路径
  3. 一般处理程序中使用session
  4. 转:EasyHook远程代码注入
  5. Android Studio Push rejected: Push to origin/Alpha1.0 was rejected
  6. *[codility]Peaks
  7. jquery mobile script
  8. CI框架多目录设置
  9. php 原生或curl获取 http headers
  10. 随机IP代理
  11. Windows Server 2016-存储新增功能
  12. python MultiProcessing模块进程间通信的解惑与回顾
  13. App.config自定义节点读取
  14. 关于python中loc和iloc方法
  15. Ubuntu18.04下配置Nginx+RTMP服务器,实现点播/直播/录制功能
  16. Django入门基础详解
  17. vb sendmessage 详解2
  18. 设置WebApi里面命名空间参数
  19. ASP.NET Core 2.0中的Azure Blob存储
  20. Kali-linux其他信息收集手段

热门文章

  1. linux下更改时区
  2. -bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_US.UTF-8): No such file or directory -bash: warning: setlocale: LC_COLLATE:
  3. laravel 标签
  4. EF只更新变化的字段
  5. Java基础——Servlet(六)分页相关
  6. Quoit Design(hdu1007)最近点对问题。模版哦!
  7. ElasticSearch 使用小结
  8. python学习之老男孩python全栈第九期_day007知识点总结
  9. js-ES6学习笔记-module(3)
  10. CentOS7下安装caffe(包括ffmpeg\boost\opencv)