字典的创建
方法1:直接创建
a = {"name":"wyl","age":"23","area":"BeiJing"}
方法2:通过元组列表换行
>>> a = [["name","wyl"],("age","23"),["area","Beijing"]]#将元组,类表转换为字典>>> b = dict(a)>>> print(b){'age': '23', 'name': 'wyl', 'area': 'Beijing'}
方法3:通过关键字创建字典
>>> c = dict(name = "wyl",age = "23",area = "BeiJing")>>> print(c){'age': '23', 'name': 'wyl', 'area': 'BeiJing'}
方法4:创建默认值为空值的字典
#方法4创建一个默认value值为‘空值’的字典>>> a = {}.fromkeys(['name','age','area'],'空值')>>> print(a){'name': '空值', 'age': '空值', 'area': '空值'}>>> a = {}.fromkeys(['name','age','area'])>>> print(a){'name': None, 'age': None, 'area': None}
2.字典格式化:format_map
>>> a = {"name":"王云龙","age":'12','area':'北京'}>>> str = "我的名字叫{name},今年{age}岁了,目前在{area}工作">>> print(str.format_map(a))我的名字叫王云龙,今年12岁了,目前在北京工作
3.字典的访问
方法1:dict[key]
>>> a = {"name":"wyl","age":"23","area":"BeiJing"}>>> print(a["name"])#此方法获取value时当key不存在时候,会报错。所以建议获取value建议使用get方法w
方法2:get方法
>>> a = {"name":"wyl","age":"23","area":"BeiJing"}>>> print(a.get('wyl'))#此方法获取value时,当key不存在时候,可指定没有key返回0如:a.get('wyl',0)None
方法3:keys,values,items遍历访问
dic = {'Name': 'Jack', 'Age': 7, 'Class': 'First'}# 1 直接遍历字典获取键,根据键取值for key in dic: print(key, dic[key])# 2 利用items方法获取键值,速度很慢,少用!for key,value in dic.items(): print(key,value)#3 利用keys方法获取键for key in dic.keys(): print(key, dic[key])#4 利用values方法获取值,但无法获取对应的键。for value in dic.values(): print(value)
3.字典的更新和添加
#key不存在dickt[key]='value'与dict.setdefault一样都是添加新值。#key值存在则会dict[key]='value'会改变原值-字典的更新。#key值存在则setdefault('key',value)方法不会改变原来的值key值存在>>> a = {"name":"wyl","age":"23","area":"BeiJing"}>>> a["admin"] = "password"#key值不存在添加>>> a.setdefault('admin123','password123')#key值不存在添加'password123'>>> print(a){'name': 'wyl', 'age': '23', 'area': 'BeiJing', 'admin': 'password', 'admin123': 'password123'}key值不存在>>> a = {"name":"wyl","age":"23","area":"BeiJing","admin":"123","admin123":'123'}>>> a["admin"] = "已更新"#key值不存在添加>>> a.setdefault('admin123','不会更新')#key值不存在添加'123'>>> print(a){'name': 'wyl', 'age': '23', 'area': 'BeiJing', 'admin': '已更新', 'admin123': '123'}字典更新字典>>> a = {"name":"wyl","age":"23","area":"BeiJing"}>>> b = {"name":"wyl","age":"24","area":"BeiJing","admin":"123","admin123":'123'}>>> a.update(b)>>> print(a){'name': 'wyl', 'age': '24', 'area': 'BeiJing', 'admin': '123', 'admin123': '123'}
4.字典的清空,及删除字典元素
>>> dic{'Name': 'Jack', 'Age': '20', 'Class': 'First', 'sex': 'male'}>>> del dic['Name'] # 删除指定的键>>> dic{'Age': '20', 'Class': 'First', 'sex': 'male'}>>> a = dic.pop('Class') # 弹出并返回指定的键>>> a'First'>>> dic{'Name': 'Jack', 'Age': 7}>>> dic.clear() # 清空字典>>> dic{}>>> del dic # 删除字典本身>>> dicTraceback (most recent call last): File "", line 1, in dicNameError: name 'dic' is not defined
5.字典的拷贝
#深/浅拷贝from copy import deepcopya = {"name":['wyl','lyw'],"age":"23","area":"BeiJing"}print('原始a',a)newaa = a.copy()#浅拷贝newa = deepcopy(a)#深拷贝a["name"][1] = "王云龙"print("修改原始a后:",a)print("浅拷贝newaa",newaa)print("深拷贝newa",newa)# 修改原始a后: {'name': ['wyl', '王云龙'], 'age': '23', 'area': 'BeiJing'}# 浅拷贝newaa {'name': ['wyl', '王云龙'], 'age': '23', 'area': 'BeiJing'}# 深拷贝newa {'name': ['wyl', 'lyw'], 'age': '23', 'area': 'BeiJing'}