字典的创建
- 循环遍历复杂,考虑使用
- 可变数据类型
- 和列表的区别:列表有序,字典无序
列表通过索引(偏移)访问元素,字典通过键(key)
-
字典基本格式:
#dic = {key1: value1, key2:value2, ... , keyn: valuen}
-
值可以是任意数据类型,键必须是不可变的,如字符串,数字或元组(不能是列表)。
-
不可哈希——可变,可哈希——不可变
空字典
dic ={}
print(type(dic))
<class ‘dict’>
创建多元素字典
info = {"id": 20190101, "name": "Tom", "age": 18}
# key: value ---> 键值对
print(info)
{‘id’: 20190101, ‘name’: ‘Tom’, ‘age’: 18}
强转
#zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。
zip()
key = ("id","name","age")
value = (20190101,"Tom",18)
tp = tuple(zip(key, value))
print(tp,type(tp))
((‘id’, 20190101), (‘name’, ‘Tom’), (‘age’, 18)) <class ‘tuple’>
fromkeys创建字典
格式:
dict.fromkeys(seq,val) # (seq字典的键,val赋给的值)
dic = dict.fromkeys(("Tom","Jack","Lucy"),666666) #seq可以用()或者[]括起来,val没有的话赋值为"None"
print(dic)
{‘Tom’: 666666, ‘Jack’: 666666, ‘Lucy’: 666666}
dict3 = dict.fromkeys([1,2,3],['one','two','three']) #[值只能是一个值]
print(dict3)
{1: [‘one’, ‘two’, ‘three’], 2: [‘one’, ‘two’, ‘three’], 3: [‘one’, ‘two’, ‘three’]}
字典的常见操作
增
- 直接添加键值对(dic[key] = value)
- 通过使用变量名[“键”] = “值”,
- 如果key 不存在,建立新键值对
- 存在则更新值
- 通过使用变量名[“键”] = “值”,
dic = {"name": "Frank"}
dic["id"] = 20190101
dic["name"] = "Tom"
print(dic)
{‘name’: ‘Tom’, ‘id’: 20190101}
-
更新dict.updata(dic)
把字典dic中的键值对更新到dict中
dic = {"name":"Jack", "Hobby":"read"} dict = {"id":20190101,"name":"Tom","age":22} dict.update(dic) print(dict)
{‘id’: 20190101, ‘name’: ‘Jack’, ‘age’: 22, ‘Hobby’: ‘read’}
删
-
dict.pop()
-
格式:dict.pop(key[,default])
key:要删除的键值,default:如果没有key,返回default值
-
如果key存在dict中,删除被删除的值
-
如果不存在,给出默认值则返回默认值,未给出则报错
dict = {"id":20190101,"name":"Tom","age":22} print(dict.pop("id",23)) print(dict)
20190101
{‘name’: ‘Tom’, ‘age’: 22}
-
-
-
dict.popitem()
- 随机删除一堆键对值
- 有返回值的,元组
dict = {"id": 20190101,"name": "Tom","age":22} print(dict.popitem()) key,value = dict.popitem() #又删了一次 print(dict) #返回值为元组 print(key,value)
(‘age’, 22)
{‘id’: 20190101}
name Tom
-
del dic[key]
dic = {"id": 20190101,"name": "Tom","age":22} del dic["age"] print(dic)
{‘id’: 20190101, ‘name’: ‘Tom’}
-
dic.clear()
dic = {"id": 20190101,"name": "Tom","age":22} print(dic.clear()) print(dic)
None
{}
改
dic = {"id":20190101,"name":"Tom","age":22}
dic["id"] = 20190102
print(dic)
{‘id’: 20190102, ‘name’: ‘Tom’, ‘age’: 22}
-
dict.setdefault()
- 键存在,不改动,返回字典中的值
- 不存在,在字典中添加相应的键值对,并返回对应的值
dic = {"id":20190101,"name":"Tom","age":22} print(dic.setdefault("name","Jack")) #键存在 print(dic) print(dic.setdefault("salary",12000)) print(dic)
Tom
{‘id’: 20190101, ‘name’: ‘Tom’, ‘age’: 22}
12000
{‘id’: 20190101, ‘name’: ‘Tom’, ‘age’: 22, ‘salary’: 12000}
查
-
字典值的访问
- 直接通过字典的key访问value
dic = {"id":20190101,"name":"Tom","age":22} print(dic["id"])
20190101
-
dict.get()访问value
- 语言格式:dict.get(key[,default])
- 不确定字典中是否存在某个键而又想对其进行操作,比如获取值–>get 方法
- 当key不存在的时候,不会抛出异常,返回None
dic = {"id":20190101,"name":"Tom","age":22} print(dic.get("id",20190201)) print(dic.get("salary")) print(dic)
-
遍历
- dict.keys()
#找键 dic = {"id":20190101,"name":"Tom","age":22} print(dic.keys(),type(dic.keys())) for key in dic.keys(): print(key, end=" ") print()
dict_keys([‘id’, ‘name’, ‘age’]) <class ‘dict_keys’>
id name age- dict.values()
for value in dic.values(): print(value, end=" ") print()
20190101 Tom 22
- dict.items()
for item in dic.items(): print(item, end=" ") print()
(‘id’, 20190101) (‘name’, ‘Tom’) (‘age’, 22)
for key,value in dic.items(): print(key, ":", value)
id : 20190101
name : Tom
age : 22