python-字典
字典dict
http://www.runoob.com/python3/python3-dictionary.html
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 } dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,只能是字符串,数字或元组。
创建
dict01={"name":"毛奇","age":42,"sex":"男"} dict02={"name":"李佩琪","age":41,"sex":"女"} dict03={"name":"马翔宇","age":32,"sex":"男"}
添加或修改
dict01={"name":"毛奇","age":42,"sex":"男"} dict01["money"]=100 #添加--尾部添加 #money是键 100是值 dict01["age"]=43 #修改值 #注意:键已经存在,就修改值;如果不存在就添加 print(dict01)
dict01={"name":"毛奇","age":42,"sex":"男"} dict02={"id":"1001","身高":171} dict01.update(dict02) #在尾部添加另一个字典的内容 print(dict01)
读取值
dict01={"name":"毛奇","age":42,"sex":"男"} if "age" in dict01: i=dict01["age"] #如果键不存在会报错 print(i)
dict01={"name":"毛奇","age":42,"sex":"男"} i=dict01.get('name', "键不存在") i1=dict01.get('Name', "键不存在") """" 参数1 键名 字典中要是存在键 就返回键对应的值;要是不存在,就返回参数2 参数2 默认是None """ print(i) print(i1)
dict01={"name":"毛奇","age":42,"sex":"男"} i=aa= dict01.setdefault('name', "祝欢") i1=dict01.setdefault('Name', "键不存在") """" 参数1 键名 字典中要是存在键 就返回键对应的值; 要是不存在,就返回参数2,并且在字典的尾部添加这个键值对 参数2 默认是None """ print(i) print(i1) print(dict01)
删除
dict01={"name":"毛奇","age":42,"sex":"男"} del dict01["age"] #删除键值对 print(dict01)
dict01={"name":"毛奇","age":42,"sex":"男"} pop=dict01.pop('name') #删除键值对,返回被删除的值,键要是不存在就抛出错误 print(dict01,pop)
dict01={"name":"毛奇","age":42,"sex":"男"} pop=dict01.pop('Name',"不存在") #删除键值对,返回被删除的值,键要是不存在就返回参数2 print(dict01,pop)
dict01={"name":"毛奇","age":42,"sex":"男"} pop=dict01.popitem() #删除最后的键值对 #返回被删除键值对的元组 print(dict01,pop)
dict01={"name":"毛奇","age":42,"sex":"男"} del dict01 #删除字典
清空字典
dict.clear()
返回字典的长度
x=len(dict)
返回键值对的总数
返回所有键和值
dict01={"name":"毛奇","age":42,"sex":"男"} a=dict01.keys() #返回所有键 #返回值:列表 b=dict01.values() #返回所有值 #返回值:列表 print(a) print(b)
遍历字典
dict01={"name":"毛奇","age":42,"sex":"男"} for item in dict01: #item是键 print(item)
dict01={"name":"毛奇","age":42,"sex":"男"} for item in dict01.keys(): #item键 print(item)
dict01={"name":"毛奇","age":42,"sex":"男"} for item in dict01.values(): #item值 print(item)
dict01={"name":"毛奇","age":42,"sex":"男"} for item in dict01.items(): #item时键值对元组 print(item)
dict01={"name":"毛奇","age":42,"sex":"男"} for key,value in dict01.items(): #key是键,value是值 print(key,value)
字典转换成列表
dict01={"name":"毛奇","age":42,"sex":"男"} lb=list(dict01) #返回键名列表 print(lb)
dict01={"name":"毛奇","age":42,"sex":"男"} lb=list(dict01.values()) #返回值列表 print(lb)
dict01={"name":"毛奇","age":42,"sex":"男"} lb=list(dict01.items()) #返回键值对的元组列表 print(lb)
列表转换成字典
lb=[('name', '毛奇'), ('age', 42), ('sex', '男')] dict01=dict(lb) #列表转换成字典 print(dict01)
练习
1.把两个列表组合成字典
#把两个列表组合成字典 list01=["毛奇","李佩霞","马翔宇"] list02=[42,43,31] dict01={} for i in range(len(list01)): key=list01[i] value=list02[i] dict01[key]=value print(dict01)
2.多层字典
dict_info={"北京":{"景区":["长城","故宫"],"美食":["烤鸭","豆汁焦圈","炸酱面"]},"四川":{"景区":["九寨沟","峨眉山"],"美食":["火锅","兔头"]}} #练习1:打印北京的第一个景区 print(dict_info["北京"]["景区"][0]) #练习2:打印所有城市 print(dict_info.keys()) #练习3:打印所有城市的所有美食(一行一个) for item in dict_info.keys(): print(dict_info[item]["美食"])
字典推导式
list01=["毛奇","李佩霞","马翔宇"] list02=[42,41,31] dict01={list01[i]:list02[i] for i in range(len(list01))} print(dict01)
其它格式:看推导式
练习:颠倒字典的键和值
dict01={'毛奇': 42, '李佩霞': 41, '马翔宇': 31} dict02={ value:key for key,value in dict01.items() } print(dict02)