pathon中字典的基本用法
字典
是一个无序,可变,和有索引的集合,在pathon中
用花括号编写, 总有键和值
实例
创建并且打印字典
thisdict = {"brand": "Porsche", "model": "911", "year": 1963 }
print(thisdict)
运行结果
{"brand": "Porsche", "model": "911", "year": 1963 }
访问项目
可以通过访问方括号中的键名来访问字典的值
实例
第一种方法
获取"year"键的值
x = thisdict["year"]
运行结果
1963
第二种方法
x=thisdict.get("year")
运行结果
1963
更改值
您可以通过访问字典的键值来更改特定的值
实例
更改"year"的值
thisdict = {"brand": "Porsche","model": "911", "year": "1963" }
thisdict["year"] = 2019
print(thisdict)
运行结果
{"brand": "Porsche", "model": "911", "year": 2019 }
遍历字典
您可以使用for循环遍历字典
返回的是字典的键
thisdict = {"brand": "Porsche","model": "911", "year":"1963" }
thisdict["year"] = 2019 for x in thisdict:
print(x)
运行结果
brand
model
year
逐个打印字典的值
for x in thisdict: print(thisdict[x])
运行结果
Porsche
911
1963
还可以通过values()函数返回字典的值
for x in thisdict.values():print(x)
运行结果
Porsche
911
1963
您要想遍历字典的键和值需要用到items()函数
for x,y in thisdict.items():print(x,y)
运行结果
brand Porsche
model 911
year 1963
检查键是否存在
您要是想确认键是否存在需要用到in关键字
实例
检验字典中是否存在"model"
thisdict = { "brand": "Porsche", "model": "911", "year": 1963}if "model" in thisdict:print("Yes, 'model' is one of the keys in the thisdict dictionary")
运行结果
Yes, 'model' is one of the keys in the thisdict dictionary
字典长度
要确定字典中存在多少键值对需要用到len()方法
print(len(thisdict))
运行结果
3
添加项目
通过使用新的索引键为其复制,可以将项目添加到字典中
thisdict = {"brand": "Porsche", "model": "911", "year": 1963}
thisdict["color"] = "red"
print(thisdict)
运行结果
{"brand": "Porsche", "model": "911", "year": 1963, "color":"red"}
删除键值对
第一种方法
实例
用pop()方法删除指定键名的键值对
thisdict = {"brand": "Porsche", "model": "911", "year": "1963"}
thisdict.pop("model")
print(thisdict)
运行结果
{"brand": "Porsche", "year": "1963"}
第二种方法
实例
可以用popitem()方法来删除最后插入的键值对
thisdict = { "brand": "Porsche","model": "911","year":"1963"}
thisdict.popitem()
print(thisdict)
运行结果
{ "brand": "Porsche","model": "911"}
第三种方法
del 关键字删除具有指定键名的键值对
thisdict ={"brand": "Porsche", "model": "911","year":1963}
del thisdict["year"]
print(thisdict)
运行结果
{ "brand": "Porsche","model": "911"}
del 关键字也可以完全删除字典
thisdict ={ "brand": "Porsche", "model": "911", "year": 1963}
del thisdict
print(thisdict)
运行结果会报错显示该字典不存在
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
print(thisdict)
NameError: name 'thisdict' is not defined
第四种方法
用clear()关键字清空字典
thisdict ={ "brand": "Porsche", "model": "911", "year": "1963"}
thisdict.clear()
print(thisdict)
运行结果
{}
该运行结果与上面不同这里是清空列表的内容
复制字典
可以用copy()的关键字来复制字典
thisdict = {"brand": "Porsche","model": "911", "year": "1963"}
mydict = thisdict.copy()
print(mydict)
运行结果
{"brand": "Porsche","model": "911", "year": "1963"}
使用dict()的方法也可以创建字典的副本
thisdict ={"brand": "Porsche","model": "911", "year": "1963"}mydict = dict(thisdict)print(mydict)
运行结果
{"brand": "Porsche","model": "911", "year": "1963"}
字典的嵌套
创建包含3个字典的字典
myfamily = {
"child1" : {
"name" : "Phoebe Adele",
"year" : 2001
},
"child2" : {
"name" : "Jennifer Katharine",
"year" : 1995
},
"child3" : {
"name" : "Rory John",
"year" : 2020
}
}
创建三个字典,然后创建一个包含其他三个字典的字典
child1 = {
"name" : "Phoebe Adele",
"year" : 2002
}
child2 = {
"name" : "Jennifer Katharine",
"year" : 1996
}
child3 = {
"name" : "Rory John",
"year" : 1999
}myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
输出结果
child1 = {
"name" : "Phoebe Adele", "year" : 2002}child2 = { "name" : "Jennifer Katharine", "year" : 1996}child3 = { "name" : "Rory John", "year" : 1999}myfamily = { "child1" : child1, "child2" : child2, "child3" : child3}
dict()构造函数
用dict来构造一个新的字典
thisdict = dict(brand="Porsche", model="911", year="1963")
print(thisdict)
输出结果
{"brand": "Porsche","model": "911", "year": "1963"}
pathon中字典的基本用法相关推荐
- Python中字典的基本用法
本文为南大张莉老师<用Python玩转数据>学习笔记 字典可以建立对象之间映射的关系,它是python中唯一内建的映射类型,字典中的每个元素是一个key-value对(键-值对),key可 ...
- python中字典del的用法_python中字典(Dictionary)用法实例详解
本文展示了字典在python中的使用.分享给大家参考.具体分析如下: 字典是一种映射结构的数据类型,由无序的"键值对"组成.字典的关键字必须是不可改变的类型,如字符串.数字和元组: ...
- python编程字典100例_python中字典(Dictionary)用法实例详解
本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的"键-值对"组成. ...
- python 字典定义日志用法_python中字典(Dictionary)用法实例详解
本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的"键-值对"组成. ...
- [转载] python里字典的用法_python中字典(Dictionary)用法实例详解
参考链接: Python字典dictionary copy方法 本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映 ...
- python中字典的使用_python中的字典用法大全
本文包含了python字典的各种用法,字典在python中的重要性不言而喻 #!/usr/bin/env python # # [代码名字: Dictionaries 101] # [代码分类: Py ...
- Python中集合set和字典dict的用法区别
Python中集合set和字典dict的用法区别 核心知识点 Python列表(list).元组(tuple).字典(dict)和集合(set)详解 Python set集合详解 1. Python创 ...
- python中字典的get函数是什么意思_python字典get()方法用法分析
本文实例讲述了python字典get()方法用法.分享给大家供大家参考.具体分析如下: 如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方 ...
- python介绍和用途-Python字典简介以及用法详解
? 1 2 3 #!/usr/bin/env python # -*- coding:utf-8 -*- """ 老规矩以下方法环境2.7.x,请3.x以上版本的朋友记得 ...
最新文章
- oracle9i在windows上的dataguard配置
- GMIS 2017大会Saman Farid演讲:人工智能时代创业者面对的挑战和机会
- python实现高校教务管理系统_基于Python技术的教务管理系统的研究与开发
- X509证书认证流程介绍
- 算法复习——虚树(消耗战bzoj2286)
- 菜鸟学习笔记:Java基础篇7(包装类、时间相关类、文件类、异常处理类)
- Dropping tests
- java统计空格代码_java统计文件中字符,数字,汉字,空格数目
- css权重机制,CSS权重及其计算
- ENSP未找到base device,是否立即注册
- 2021年道路运输企业主要负责人模拟考试题库及道路运输企业主要负责人实操考试视频
- 文华学院计算机专业师资,华中科技大学文华学院“最受欢迎教师”名单
- 340. 至多包含K个不同字符的最长子串
- poi读取Excel时日期为数字 的解决方法
- 计算机B级基金有哪些类型,分级b基金是什么
- 量化投资学习——股指期货研究(六)
- 智力题:一次测试找出1000瓶酒中的唯一一瓶毒酒
- Promise.all的用法及其细节
- Change to survive
- springboot+微信小程序的点餐系统(开题报告+论文+答辩PPT+源码)
热门文章
- windows10系统,如何进行文件内容多关键字搜索
- Python练习:炉石传说荣誉室返尘最优策略
- 【51单片机】十分钟学会定时器中断¹
- 二重积分x^2+y^2_计算二重积分∫∫(x+y)dxdy,其中D为x^2+y^2≤2x
- 经纬度数据计算-JavaScript
- java utc时间_Java获取UTC时间的方法
- 【教程】如何利用patchrom来编译你自己的MIUI
- Vue <transition> 多个组件的过渡案例
- (记录)golang获取mongo的ObjectId
- ueditor编辑器上传图片的显示问题