1、创建字典的方法
1)x = {“吕布”:“口口布”, “关羽”:“关习习”}
2)使用dict函数
y = dict(吕布=“口口布”, 关羽=“关习习”)
3)使用列表作为参数
z = dict([(“吕布”,“口口布”),(“关羽”,“关习习” )])
4)d = dict({“吕布”:“口口布”, “关羽”:“关习习”})
5) e = dict({“吕布”:“口口布”, “关羽”:“关习习”}, 刘备=“刘baby”)
6) f = dict(zip([“吕布”, “关羽”], [“口口布”, “关习习”]))
2、字典常用方法
fromkeys(iterable[, values]):初始化一个字典
d = dict.fromkeys(“Fish”, 250)
d
{‘F’: 250, ‘i’: 250, ‘s’: 250, ‘h’: 250}
d[“F”] = 520
d
{‘F’: 520, ‘i’: 250, ‘s’: 250, ‘h’: 250}
d[“c”] = 67
d
{‘F’: 520, ‘i’: 250, ‘s’: 250, ‘h’: 250, ‘c’: 67}
pop:删除字典中的元素,语法 pop(key[, values])
示例代码片段1
d = dict.fromkeys(“Fish”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
d.pop(“h”)
256
d
{‘F’: 256, ‘i’: 256, ‘s’: 256}
删除一个不存在的键就会抛出异常,如果需要不抛出异常的会可以给定一个默认值
d = dict.fromkeys(“Fish”,256)
d.pop(“c”)
Traceback (most recent call last):
File “<pyshell#95>”, line 1, in
d.pop(“c”)
KeyError: ‘c’
d.pop(“c”, “键不存在”)
‘键不存在’
popitem():删除最后一个加入字典的键值对
d = dict.fromkeys(“Fish”,256)
d[“c”] = 234
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256, ‘c’: 234}
d.popitem()
(‘c’, 234)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
d.popitem()
(‘h’, 256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256}
del 删除指定的键
d = dict.fromkeys(“Fish”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
del d[“F”]
d
{‘i’: 256, ‘s’: 256, ‘h’: 256}
删除整个字典
del 字典名
d = dict.fromkeys(“Fish”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
del d
d
Traceback (most recent call last):
File “<pyshell#134>”, line 1, in
d
NameError: name ‘d’ is not defined
清除整个字典里面的内容变成一个空字典
d = dict.fromkeys(“Fish”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
d.clear()
d
{}
修改字典键值的方法
update([other])
示例1:传入一个字典做为参数覆盖原有字典
d = dict.fromkeys(“Fish”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256}
d.update({“F”:“13”, “i”:“14”}) #也可写成d.update(F=“13”)
d
{‘F’: ‘13’, ‘i’: ‘14’, ‘s’: 256, ‘h’: 256}
get(key[, default]):查找字典中键对应的值
示例1
d = dict.fromkeys(“Fish”,256)
d.get(“c”, “字典中不存在该键”)
‘字典中不存在该键’
d.get(“F”)
256
setdefault(key[, value]):查找字典中是否存在某个键,存在则返回其值,不存在则添加这个键值对
示例:
d = dict.fromkeys(“FishC”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256, ‘C’: 256}
d.setdefault(“B”, “456”)
‘456’
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256, ‘C’: 256, ‘B’: ‘456’}
d.setdefault(“F”, “743”)
256
items()、keys()、values():分别用于获取字典的键值对、键和值三者的视图对象(何为视图对象:字典内容发生改变时,它们三也跟着改变)
示例1:
d = dict.fromkeys(“FishC”,256)
d
{‘F’: 256, ‘i’: 256, ‘s’: 256, ‘h’: 256, ‘C’: 256}
keys = d.keys()
items = d.items()
values = d.values
keys
dict_keys([‘F’, ‘i’, ‘s’, ‘h’, ‘C’])
values
<built-in method values of dict object at 0x00000136054EA4C8>
values = d.values()
values
dict_values([256, 256, 256, 256, 256])
items
dict_items([(‘F’, 256), (‘i’, 256), (‘s’, 256), (‘h’, 256), (‘C’, 256)])
d.pop(“F”)
256
d
{‘i’: 256, ‘s’: 256, ‘h’: 256, ‘C’: 256}
items
dict_items([(‘i’, 256), (‘s’, 256), (‘h’, 256), (‘C’, 256)])
keys
dict_keys([‘i’, ‘s’, ‘h’, ‘C’])
values
dict_values([256, 256, 256, 256])
copy():浅拷贝一个字典
用法:e = 字典名.copy()
len():返回字典键值对的长度
in 、not in:判断某个键值是否在字典中
list(字典名):将字典的键返回一个列表
list(字典名.values()):将字典键对应的值返回一个列表
e = iter(字典名):将字典变为迭代器
字典推导式:
d = {“F”:70, “i”:105}
b = {v:k for k,v in d.items()}
b
{70: ‘F’, 105: ‘i’}