文章目录
字典Dictory创建一个字典一个普通字典一个嵌套字典dict() 函数来创建字典(不常用)字典操作增直接增加使用setdefault() 函数进行增加删del() 直接删除某个键下和值;如果直接跟字典,则整个字典都删除clear() 直接将所有的键值对儿全部清空,就留下一个空字典pop() 将相应的键值对儿删除,将key所对应的value 做为返回值进行返回popitem() 随机删除对应的键值对儿,将key与value都做为元组方式做为返回值查.values() 查看所有的值.keys() 查出所有的键.items() 查看所有的键跟值,全部查出来改. update()其它操作dict.fromkeys() : 指定多个key,然后统一赋值,可用于初始化字典嵌套字典嵌套后内容的修改字典排序字典遍历字典Dictory
通过key-value的形式存储数据。python 对key进行哈希计算,计算的结果是内存的地址用于存储value的值。字典是无序存储。字典的取值,存值通过哈希的结果进行存取列表 其实类似C放言中说的数组,是一段连续的内存空间,查找,删除,插入数据时需要通过下标的偏移量进行处理,具有查找快。字典不是连接的内存空间,而是将key进行哈希后得到内存地址进行存储。因需要哈希存储,所以key必需唯一,如果key相同则得到同一个内存地址,将会冲突“key”必需是不可变的类型,像字符串,整型,元组等,像列表,字典是可变的类型,是不可哈希的。“value”可以是可变的类型,可以是列表,元组,或者 字典都可以。创建一个字典
一个普通字典
dictory = {"name":"taohuadaozhu","age":30,"gendle":"male","company":"baidu","department":"IT"}
dictory : 变量名{ } :使用大括号括起来“key”:“value”: 一个键值对中间使用冒号连接, 不同的键值对之间使用“,”隔开
一个嵌套字典
dictory = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}
dict() 函数来创建字典(不常用)
dictory = dict((("name","feihuang"),("age",30)))
一对key,value用一个()括起来,也可以使用[ ]key与value之间使用“,”隔开不同的key,value对儿用一个括号对儿括号括起来
字典操作
增
有两种方法:
直接增加
因为字典是无序的,所以不需要指定什么,只需要指定增加的key即可如果键是已经存在的,则直接将value进行更改成新的指定的值使用setdefault() 函数进行增加
如果增加的是新的key,则直接增加。如果增加的是已经存在的key,则不会更改原来的值,相对安全setdefault() 方法是具有返回值的,可以保存到另外一个变量中用于返回是否成功修改等作用dictory = {"name":"taohuadaozhu","age":30}print(dictory)#增加一对键值dictory["company"]="baidu"print(dictory)#增加时键为已经存在的键,如果用此种方法,则是直接将原来的值 进行了更改dictory["age"]=18print(dictory)#使用setdefault() 方法来增加dictory.setdefault("department","IT")print(dictory)#使用setdefault() 函数对已经存在的key进行更改dictory.setdefault("age",20)print(dictory)"""{'name': 'taohuadaozhu', 'age': 30}{'name': 'taohuadaozhu', 'age': 30, 'company': 'baidu'}{'name': 'taohuadaozhu', 'age': 18, 'company': 'baidu'}{'name': 'taohuadaozhu', 'age': 18, 'company': 'baidu', 'department': 'IT'}{'name': 'taohuadaozhu', 'age': 18, 'company': 'baidu', 'department': 'IT'}"""
删
del() 直接删除某个键下和值;如果直接跟字典,则整个字典都删除
dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}print(dictory_baidu)del dictory_baidu["gendle"]print(dictory_baidu)
dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}del dictory_baiduprint(dictory_baidu)
clear() 直接将所有的键值对儿全部清空,就留下一个空字典
dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}dictory_baidu.clear()print(dictory_baidu)
pop() 将相应的键值对儿删除,将key所对应的value 做为返回值进行返回
dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}name = dictory_baidu.pop("name")print(name)print(dictory_baidu)
popitem() 随机删除对应的键值对儿,将key与value都做为元组方式做为返回值
dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}name = dictory_baidu.popitem()print(name)print(dictory_baidu)
查
.values() 查看所有的值
.keys() 查出所有的键
.items() 查看所有的键跟值,全部查出来
dictory = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}#查出所有的键(key),但是查出来的并不是一个列表,但是可以转换成列表print(dictory.keys()) #查看dictory这个字典的所有的键print(dictory.values())# 查看所有的valueprint(dictory.items())#显示所有的键值对儿print(type(dictory.keys())) #查看dictory.keys() 数据类型dictory_key = list(dictory.keys()) #强制转换为列表,并将返回值 赋值给dictory_key这个变量dictory_value = list(dictory.values()) #将values强制转换为列表print(type(dictory_key)) #查看dictory_key的数据类型,这里为list列表了。print(type(dictory_value)) #查看dictory_value的数据类型,这里为list列表了。"""以下为输出内容dict_keys(['name', 'age', 'gendle', 'company', 'department'])dict_values(['taohuadaozhu', 30, 'male', {'baidu': 'Bigfirm', 'character': 'Privately owned'}, 'IT'])dict_items([('name', 'taohuadaozhu'), ('age', 30), ('gendle', 'male'), ('company', {'baidu': 'Bigfirm', 'character': 'Privately owned'}), ('department', 'IT')])<class 'dict_keys'><class 'list'><class 'list'>"""
改
. update()
可以将一个字典update到另外一个字典中去,将一个字典合并到另外一个字典中去。如果两个字典中的key有重复的,则将更新后的值覆盖更新前的值如果原字典中没有的key,则加入到字典中去dictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}dictory_xiaomi = {"iname":"feihuang","iage":29,"gendle":"male","company_cn":"xiaomi","department":"IT"}#修改其中一个值,与前面讲的增加一样,如果已经有的key,则改key所对应的值,如果没有这个key,则直接修改其内容dictory_baidu["age"]=18print(dictory_baidu)print(dictory_xiaomi)#使用update()方法对字典进行更新dictory_baidu.update(dictory_xiaomi) # 将dictory_xiaomi合并到dictory_baidu中去,如果已原字典中已经有相同的key了,则直接替换为dictory_xiaomi中对应的key的value,如果没有key,则直接合并过去print(dictory_baidu)print(dictory_xiaomi)"""{'name': 'taohuadaozhu', 'age': 18, 'gendle': 'male', 'company': {'baidu': 'Bigfirm', 'character': 'Privately owned'}, 'department': 'IT'}{'iname': 'feihuang', 'iage': 29, 'gendle': 'male', 'company_cn': 'xiaomi', 'department': 'IT'}{'name': 'taohuadaozhu', 'age': 18, 'gendle': 'male', 'company': {'baidu': 'Bigfirm', 'character': 'Privately owned'}, 'department': 'IT', 'iname': 'feihuang', 'iage': 29, 'company_cn': 'xiaomi'}{'iname': 'feihuang', 'iage': 29, 'gendle': 'male', 'company_cn': 'xiaomi', 'department': 'IT'}"""
其它操作
dict.fromkeys() : 指定多个key,然后统一赋值,可用于初始化
dictory = dict.fromkeys(["name1","name2","name3","name4"],"172.16.100.1")print(dictory)dictory = dict.fromkeys(["name1","name2","name3","name4"],["172.16.100.1",""])print(dictory)"""{'name1': '172.16.100.1', 'name2': '172.16.100.1', 'name3': '172.16.100.1', 'name4': '172.16.100.1'}{'name1': ['172.16.100.1', ''], 'name2': ['172.16.100.1', ''], 'name3': ['172.16.100.1', ''], 'name4': ['172.16.100.1', '']}"""
字典嵌套
字典内套着字典,但是也不限仅于此,也可以嵌套像列表,元组等都可以
dictory = {"Wudang":{"name":"ZhangSanfeng","Age":103,"Skill":"Taiji"},"Emei":{"name": "Miejueshitai","Age": 73,"Skill": "Emeijianfa"},"HuaShan":{"name": "Fengqingyang","Age": 53,"Skill": "Dugujiujian"},"Mojiao":{"name": "Dongfangbubai","Age": 63,"Skill": "xixingdafa"}}print(dictory)“”“#以下为输出的内容{'Wudang': {'name': 'ZhangSanfeng', 'Age': 103, 'Skill': 'Taiji'}, 'Emei': {'name': 'Miejueshitai', 'Age': 73, 'Skill': 'Emeijianfa'}, 'HuaShan': {'name': 'Fengqingyang', 'Age': 53, 'Skill': 'Dugujiujian'}, 'Mojiao': {'name': 'Dongfangbubai', 'Age': 63, 'Skill': 'xixingdafa'}}”“”
字典嵌套后内容的修改
dictory["Wudang"]["name"]="zhangwuji"print(dictory)"""{'Wudang': {'name': 'zhangwuji', 'Age': 103, 'Skill': 'Taiji'}, 'Emei': {'name': 'Miejueshitai', 'Age': 73, 'Skill': 'Emeijianfa'}, 'HuaShan': {'name': 'Fengqingyang', 'Age': 53, 'Skill': 'Dugujiujian'}, 'Mojiao': {'name': 'Dongfangbubai', 'Age': 63, 'Skill': 'xixingdafa'}}"""
字典排序
这里默认情况下是根据键进行排序,如需要根据值进行排序,可以"sorted(字典.value())"列表里面是根据值进行排序
字典遍历
一、只打印keydictory_baidu = {"name":"taohuadaozhu","age":30,"gendle":"male","company":{"baidu":"Bigfirm","character":"Privately owned"},"department":"IT"}for i in dictory_baidu:print(i)print("==================================================")"""nameagegendlecompanydepartment=================================================="""
打印key与对应的value,此方法效率高
for i in dictory_baidu:print(i,dictory_baidu[i])print("==================================================")"""name taohuadaozhuage 30gendle malecompany {'baidu': 'Bigfirm', 'character': 'Privately owned'}department IT=================================================="""
第二种方法打印key与对应的value,但是输出的是元组
for i in dictory_baidu.items():print(i)print("==================================================")"""('name', 'taohuadaozhu')('age', 30)('gendle', 'male')('company', {'baidu': 'Bigfirm', 'character': 'Privately owned'})('department', 'IT')=================================================="""
第三种方法打印key与对应的value,使用两个变量输出
for i,v in dictory_baidu.items():print(i,v)print("==================================================")"""name taohuadaozhuage 30gendle malecompany {'baidu': 'Bigfirm', 'character': 'Privately owned'}department IT=================================================="""
如果觉得《Python Basic - 字典 (Dictionary) 的使用方法 相关操作 增删查改 字典嵌套 字典遍历 字典排序》对你有帮助,请点赞、收藏,并留下你的观点哦!