一. 字典的简单介绍
字典是{}括起来的键值对组成dict = {key1:value1,key2:value2,...}. 在dict中key是唯一的. 在dict中的key必须是不可变的. dict保存的数据不是按照我们添加进去的顺序保存的.
dic = {"one":1, "two":2, "three":3} dic["four"] = 4 #把"four":4添加进字典 print(dic) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘four‘: 4}.
dic = {["work","job"]:"long"} print(dic) #TypeError: unhashable type: ‘list‘ list是可变的不能作为key
二. 字典的增删改查和其他操作
1. 增加
dic = {"one":1, "two":2, "three":3} dic["four"] = 4 #把"four":4添加进字典 print(dic) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘four‘: 4}.
dic = {"one":1, "two":2, "three":3} dic["two"] = "二" #当key重复时,会更新value的值 print(dic) #{‘one‘: 1, ‘two‘: ‘二‘, ‘three‘: 3}
dic = {"one":1, "two":2, "three":3} dic.setdefault("five",5) #把key值为"five",value值为5.添加到dic字典里 print(dic) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘five‘: 5} dic.setdefault("six",6) print(dic) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘five‘: 5,‘six‘: 6} dic.setdefault("six",7) #已经有key值为"six",所以此次不存储 print(dic) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘five‘: 5, ‘six‘: 6}
2.删除
pop(key) 通过key值删除,删除一个元素. 返回这个元素的value值
del dic[key] 通过key值删除
popitem() 随机删除,删除一个元素. 返回一个元组
clear() 清空字典
dic = {"one":1, "two":2, "three":3} ret = dic.pop("two") #删除key值为"two"的元素 print(dic) #{‘one‘: 1, ‘three‘: 3} print(ret) #2 key值为"two"的value为2
dic = {"one":1, "two":2, "three":3} del dic["one"] print(dic) 3{‘two‘: 2, ‘three‘: 3}
dic = {"one":1, "two":2, "three":3} a = dic.popitem() print(a) #(‘three‘, 3) 返回的是一个元组 print(dic) #{‘one‘: 1, ‘two‘: 2}
dic = {"one":1, "two":2, "three":3} dic.clear() print(dic) #{}
3. 修改
update()是更新的意思,可以在两个字典里,把一个字典的元素更新到另一个字典里.不同的key值,直接把元素添加到另一个字典中,若有相同的key值,则把value值覆盖.达到了修改的作用.
dic1 = {"one": 1, "two": 2, "three": 3, "four": "四"} dic2 = {"one": "一", "four": 4, "five": 5, "two": "二"} dic3 = {"five": "五", "six": "六", "seven": 7} dic1.update(dic2) dic2.update(dic3) dic3.update(dic2) print(dic1) #{‘one‘: ‘一‘, ‘two‘: ‘二‘, ‘three‘: 3, ‘four‘: 4, ‘five‘: 5} print(dic2) #{‘one‘: ‘一‘, ‘four‘: 4, ‘five‘: ‘五‘, ‘two‘: ‘二‘, ‘six‘: ‘六‘, ‘seven‘: 7} print(dic3) #{‘five‘: ‘五‘, ‘six‘: ‘六‘, ‘seven‘: 7, ‘one‘: ‘一‘, ‘four‘: 4, ‘two‘: ‘二‘}
4.查询
可以直接查询dic[key],也可以用get(key)和setdefault(key,value).
dic1 = {"one": 1, "two": 2, "three": 3, "four": "四"} print(dic1["four"]) #四 print(dic1.get("on")) #None 找不到key值,返回None,也可以自己设置返回值dic1.get(key,返回值). ret = dic1.setdefault("five",5) print(dic1) #{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘four‘: ‘四‘, ‘five‘: 5} print(ret) #5
5. 其它相关操作
keys() 打印出所有的key值
values() 打印出所有的value值
items() 打印出所有键值对.
dic = {"name":"tom","sex":"male","age":25,"job":"actor"} print(dic.keys()) #dict_keys([‘name‘, ‘sex‘, ‘age‘, ‘job‘]) 高仿列表,不是列表 for c in dic.keys(): print(c) # name # sex # age # job
dic = {"name":"tom","sex":"male","age":25,"job":"actor"} print(dic.values()) #dict_values([‘tom‘, ‘male‘, 25, ‘actor‘]) for c in dic.values(): print(c) # tom # male # 25 # actor
dic = {"name":"tom","sex":"male","age":25,"job":"actor"} print(dic.items()) #dict_items([(‘name‘, ‘tom‘), (‘sex‘, ‘male‘), (‘age‘, 25), (‘job‘, ‘actor‘)]) for c in dic.items(): print(c) # (‘name‘, ‘tom‘) # (‘sex‘, ‘male‘) # (‘age‘, 25) # (‘job‘, ‘actor‘) print(c[0]) #打印元组c的第一个元素 print(c[1]) #打印元组c中的第二个元素
key,value = ("name", "tom") print(key) #name print(value) #tom #key,value对应name,tom,我们key把上面for循环的c直接换成key,value for key,value in dic.items(): print(key,value) # name tom # sex male # age 25 # job actor
三. 字典的嵌套
字典的嵌套是通过找key值来打印出value值,先从最外层找起.
dic = {"time":8,"place":"park","people":{"name":"tom","sex":"male","age":20} ,"food":{"China":"rice","America":"steak","Britain":"tea"}} print(dic["people"]["sex"]) #male print(dic["food"]["Britain"]) #tea
原文:https://www.cnblogs.com/huangqihui/p/9274756.html