1.字典是无序、可变、可迭代的数据结构,存储键值对。
键值必须是唯一的,可以是数字、元组、字符串;值可以任意
高效性;可扩展性
2.查找和删除
dict1 = {"章叁":89,"李斯":75}
print(dict1)
print(dict1["章叁"])
# 参数--要查找的键,查找不到要输出的内容
#查找不到不会报错
print(dict1.get("章叁","出错啦!"))
#字典的修改
dict1["李斯"] = 99 # {'章叁': 89, '李斯': 99}
dict1["李说"] = 98 # {'章叁': 89, '李斯': 99, '李说': 98}
# 多次更新多个
dict1.update({"高盼":88,"魏焕":76,"李斯":79}) #{'章叁': 89, '李斯': 79, '李说': 98, '高盼': 88, '魏焕': 76}
# 字典的删除
del dict1["高盼"] # {'章叁': 89, '李斯': 79, '李说': 98, '魏焕': 76}
print(dict1.pop("章叁","出错啦!")) # 89
print(dict1.pop("章叁","出错啦!")) # 出错啦!
3.
dict1 = {'章叁': 89, '李斯': 79, '李说': 98, '高盼': 88, '魏焕': 76}
print(len(dict1))
# 返回所有的值
print(dict1.values())
# 返回所有的键
print(dict1.keys())
# 返回所有的键值对
print(dict1.items())
#遍历字典
for key in dict1:
print(key,dict1[key])
638

被折叠的 条评论
为什么被折叠?



