字典
Python中的字典是通过Key找到其对应的Value,Key不可以重复(重复添加等同于覆盖原有数据),Value可以重复
字典的定义,同样使用{},不过存储的元素是一个个的:键值对,如下语法
- 定义字典字面量 {key: value, key: value, ...key: value}
- 定义字典变量 my_dict = {key: value, key: value, ...key: value}
- 定义空字典 my_dict = {} 或 my_dict = dict()
{"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99}
my_dict = dict()
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99}
print(my_dict)
# 定义重复key
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(my_dict)
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 99}
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 88}
- 字典数据的获取
字典同集合一样,不可以使用下标索引,但是字典可以通过key值来取得对应的value
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(my_dict["tang4"])
88
- 字典的嵌套
字典的key和value可以是任意数据类型(Key不可为字典)
姓名 语文 数学 英语
tang1 80 88 78
tang2 85 78 88
tang3 87 87 85
stu_score_dict = {
"姓名":{
"tang1":{
"语文": 80,
"数学": 88,
"英语": 78
},
"tang2":{
"语文": 85,
"数学": 78,
"英语": 88
},
"tang3":{
"语文": 87,
"数学": 87,
"英语": 85
}
}
}
stu_score_dict["姓名"]["tang3"]["语文"]
87
字典的常用操作
- 新增元素/更新元素
语法:字典[Key]=Value,结果:字典被修改,新增了元素,对于已经存在的Key,更新其Value值
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
my_dict["tang5"] = 90
print(my_dict)
my_dict["tang4"] = 99
print(my_dict)
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 88, 'tang5': 90}
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 99, 'tang5': 90}
- 删除元素
语法:字典.pop(Key),结果:获得指定Key的Value,同时字典被修改,指定的Key的数据被删除
语法:del 字典[key] 结果:删除指定key及value
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
test_value = my_dict.pop("tang4")
print(test_value)
print(my_dict)
del my_dict["tang2"]
print(my_dict)
88
{'tang1': 96, 'tang2': 97, 'tang3': 98}
{'tang1': 96, 'tang3': 98}
- 清空字典
语法:字典.clear(),结果:字典被清空
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
print(my_dict)
my_dict.clear()
print(my_dict)
{'tang1': 96, 'tang2': 97, 'tang3': 98, 'tang4': 88}
{}
- 获取全部的Key
语法:字典.Key(),结果:得到字典中的全部Key
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
keys = my_dict.keys()
print(keys)
dict_keys(['tang1', 'tang2', 'tang3', 'tang4'])
- 获取全部的Value
语法:字典.values(),结果:得到字典中的全部Value
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
values = my_dict.values()
print(values)
dict_values([96, 97, 98, 88])
- 字典的遍历
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
keys = my_dict.keys()
values = my_dict.values()
for i in keys:
print(f"字典的key是:{i},它的value值是:{my_dict[i]}")
字典的key是:tang1,它的value值是:96
字典的key是:tang2,它的value值是:97
字典的key是:tang3,它的value值是:98
字典的key是:tang4,它的value值是:88
# 字典遍历取的就是字典的key值
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
for i in my_dict:
print(f"字典的key是:{i},它的value值是:{my_dict[i]}")
字典的key是:tang1,它的value值是:96
字典的key是:tang2,它的value值是:97
字典的key是:tang3,它的value值是:98
字典的key是:tang4,它的value值是:88
- 统计字典的元素数量
my_dict = {"tang1": 96, "tang2": 97, "tang3": 98, "tang4": 99, "tang4": 88}
count = len(my_dict)
print(count)
4
{'tang2': 97, 'tang3': 98, 'tang4': 88}