标签:perm 休息 需要 清空 报错 基础 expec print too
字典是可变类型数据,可迭代数据类型,无序的数据结构
字典可以存储大量的数据,存储量要比列表还大
字典利用键值对将数据关联起来
dic = {"ZXD": "hobby", "hobby": "play", "play": "DNF"}
s = dic[dic[dic["ZXD"]]]
print(s)
字典是根据key查找value,所以字典的查找速度要比列表快得多的多
字典所有的操作都是针对key进行的
字典的键是可哈希的(不可变类型),且唯一的,定义字典时若输入重复的键,取最后一个键值对
dict[key] = value | 利用key添加键值对 |
---|---|
dict.setdefault(key, value) | 若字典中无key,则添加key:value |
dict[key] = value
dic = {"name": "ZXD", "age": 23, "sex": "man"}
dic["hobby"] = "漂亮年轻的小姐姐"
print(dic)
# {'name': 'ZXD', 'age': 23, 'sex': 'man', 'hobby': '漂亮年轻的小姐姐'}
dict.setdefault(key, value)
dic = {"name": "ZXD", "age": 23, "sex": "man"}
res1 = dic.setdefault("age", 18)
res2 = dic.setdefault("hobby", "漂亮年轻的小姐姐")
print(res1) # 23
print(res2) # 漂亮年轻的小姐姐
print(dic)
# {'name': 'ZXD', 'age': 23, 'sex': 'man', 'hobby': '漂亮年轻的小姐姐'}
dict.pop(key) | 根据key删除键值对 |
---|---|
dict.popitem() | 随即删除键值对 |
dict.clear() | 清空字典 |
del | 各种删除 |
dict.pop(key)
dic = {"name": "ZXD", "age": 23, "sex": "man"}
det = dic.pop("sex")
print(det) # man
print(dic) # {'name': 'ZXD', 'age': 23}
dict.popitem()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
det = dic.popitem()
print(det) # ('sex', 'man')
print(dic) # {'name': 'ZXD', 'age': 23}
dict.clear()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
dic.clear()
print(dic) # {}
del
dic = {"name": "ZXD", "age": 23, "sex": "man"}
del dic["sex"]
print(dic) # {'name': 'ZXD', 'age': 23}
del dic
print(dic) # NameError: name 'dic' is not defined
dict[key] = value | 利用key修改value |
---|---|
dict1.update(dict2) | 将dict2更新到dict1中 |
dict[key] = value
dic = {"name": "ZXD", "age": 23, "sex": "man"}
dic["sex"] = "superman"
print(dic) # {'name': 'ZXD', 'age': 23, 'sex': 'superman'}
dict1.update(dict2)
dic1 = {"name": "ZXD", "age": 23}
dic2 = {"sex": "man", "age": 18}
dic1.update(dic2)
print(dic1) # {'name': 'ZXD', 'age': 18, 'sex': 'man'}
dict[key] | 通过key查看value |
---|---|
dict.get() | 查询key对应的值 |
dict.setdefault(key, value) | 查询key对应的值 |
dict[key]
dic = {"name": "ZXD", "age": 23, "sex": "man"}
print(dic["name"]) # ZXD
print(dic["hobby"]) # KeyError: 'hobby'
dict.get()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
res1 = dic.get("name")
res2 = dic.get("hobby", "没有爱好")
print(res1) # ZXD
print(res2) # 没有爱好
dict.setdefault(key, value)
dic = {"name": "ZXD", "age": 23, "sex": "man"}
res1 = dic.setdefault("age", 18)
res2 = dic.setdefault("hobby", "漂亮年轻的小姐姐")
print(res1) # 23
print(res2) # 漂亮年轻的小姐姐
print(dic)
# {'name': 'ZXD', 'age': 23, 'sex': 'man', 'hobby': '漂亮年轻的小姐姐'}
解构
a, b, c = 1, 2, 3 # 解构的过程
print(a)
print(b)
print(c)
a, b = 1, 2, 3, 4
print(a, b) # ValueError: too many values to unpack (expected 2)
a, *b = 1, 2, 3, 4
print(a, b) # 1 [2, 3, 4]
print(a, *b) # 1 2 3 4
dict.keys() | 得到所有键 |
---|---|
dict.value() | 得到所有值 |
dict.items() | 得到所有键值对 |
dict.keys()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
print(dic.keys()) # dict_keys(['name', 'age', 'sex'])
for el in dic.keys():
print(el, end=" ") # name age sex
dict.value()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
print(dic.values()) # dict_values(['ZXD', 23, 'man'])
for el in dic.values():
print(el, end=" ") # ZXD 23 man
dict.items()
dic = {"name": "ZXD", "age": 23, "sex": "man"}
print(dic.items())
# dict_items([('name', 'ZXD'), ('age', 23), ('sex', 'man')])
for el in dic.items():
print(el, end=" ")
# ('name', 'ZXD') ('age', 23) ('sex', 'man')
dic = {"name": "ZXD", "age": 23, "sex": "man"}
for key, value in dic.items():
print(key, value)
运行结果:
name ZXD
age 23
sex man
核心思想:无论嵌套多少字典,一层一层利用key就可以找到value
dic = {
"7点": {"晨练": ["跑步", "拉伸"]},
"7点45": {"洗漱": ["洗头", "刷牙"], "吃早餐": ["面包", "牛奶"]},
"8点30": {"上课": {"课前": ["回顾", "预习"], "上课": "认真听讲"}, "下课": ["休息", "敲代码"]},
"12点30": ["吃午饭", "午休"],
"14点30": {"自习": ["整理笔记", "写作业"]},
"18点30": ["吃晚饭", "休息"],
"19点30": {"自习": ["预习", "看视频"]},
}
# 想找到"敲代码"
print(dic["8点30"]["下课"][1]) # 敲代码
标签:perm 休息 需要 清空 报错 基础 expec print too
原文地址:https://www.cnblogs.com/tianlangdada/p/11518656.html