Python dict字典 定义 创建 增 删 改 查 合并
定义如下
-
映射类型Mapping Types — dict 映射对象将可散列值映射到任意对象。 映射是可变对象。 目前只有一种标准映射类型,即字典。
-
字典的键几乎是任意值。 不可散列的值,即包含列表、字典或其他可变类型(按值而不是按对象标识进行比较)的值不能用作键。 用于键的数字类型遵循数字比较的正常规则:如果两个数字比较相等(例如 1 和 1.0),那么它们可以互换使用以索引相同的字典条目。 (但是请注意,由于计算机将浮点数存储为近似值,因此将它们用作字典键通常是不明智的。)
-
可以通过将逗号分隔的
key:value对列表放在大括号内来创建字典,例如:{\' jack\': 4098, \'sjoerd\': 4127}或{4098:\'jack\', 4127: \'sjoerd\'},或通过dict构造函数。 -
返回一个从可选位置参数和一组可能为空的关键字参数初始化的新字典。
创建字典
-
使用逗号分隔的键:大括号内的值对列表:
{\'jack\': 4098, \'sjoerd\': 4127}或{4098: \'jack\', 4127: \'sjoerd\'} -
使用字典推导式:
{},{x: x ** 2 for x in range(10)} -
使用类型构造函数:
dict(),dict([(\'foo\', 100), (\'bar\', 200)]),dict(foo=100, bar=200)
>>> {x: x ** 2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>> dict([(\'foo\', 100), (\'bar\', 200)])
{\'foo\': 100, \'bar\': 200}
- 如果没有给出位置参数,则创建一个空字典。 如果给出了一个位置参数并且它是一个映射对象,则使用与映射对象相同的键值对创建一个字典。 否则,位置参数必须是可迭代对象。 可迭代对象中的每个项目本身必须是一个恰好有两个对象的可迭代对象。 每个项目的第一个对象成为新字典中的键,第二个对象成为相应的值。 如果某个键出现多次,则该键的最后一个值将成为新字典中的相应值。
- 如果给出了关键字参数,则关键字参数及其值将添加到从位置参数创建的字典中。 如果要添加的键已经存在,则来自关键字参数的值将替换来自位置参数的值。
下面的例子都返回一个等于{"one": 1, "two": 2, "three": 3}的字典:
>>> a = dict(one=1, two=2, three=3)
>>> b = {\'one\': 1, \'two\': 2, \'three\': 3}
>>> c = dict(zip([\'one\', \'two\', \'three\'], [1, 2, 3]))
>>> d = dict([(\'two\', 2), (\'one\', 1), (\'three\', 3)])
>>> e = dict({\'three\': 3, \'one\': 1, \'two\': 2})
>>> f = dict({\'one\': 1, \'three\': 3}, two=2)
>>> a == b == c == d == e == f
True
字典操作
-
list(d):返回字典 d 中使用的所有键的列表。 -
len(d):返回字典 d 中的项目数。 -
d[key]:用key键 返回d的项。 如果 key 不在map中,则引发KeyError。
>>>a = {"one": 1, "two": 2, "three": 3}
>>> list(a)
[\'one\', \'two\', \'three\']
>>> len(a)
3
>>> a[\'one\']
1
-
d[key] = value将 d[key] 设置为 value。 -
del d[key]从 d 中删除 d[key]。 如果 key 不在地图中,则引发 KeyError。
>>> a = {"one": 1, "two": 2, "three": 3}
>>> a[\'one\'] = 4
>>> a
{\'one\': 4, \'two\': 2, \'three\': 3}
>>> del a["one"]
>>> list(a)
[\'two\', \'three\']
-
key in d如果 d 有一个key键,则返回 True,否则返回 False。 -
key not in d等价于 not key in d。 -
iter(d)在字典的键上返回一个迭代器。 这是 iter(d.keys()) 的快捷方式。 -
copy()返回字典的浅拷贝。 -
clear()从字典中删除所有项目。
>>> a = {"one": 1, "two": 2, "three": 3}
>>> \'one\' in a
True
>>> \'one\' not in a
False
>>> [x for x in iter(a)]
[\'one\', \'two\', \'three\']
>>> a.copy()
{\'one\': 1, \'two\': 2, \'three\': 3}
>>> a.clear()
{}
-
classmethod fromkeys(iterable[, value])创建一个新字典,其中的键来自iterable,值设置为value。fromkeys()是一个返回新字典的类方法。 值默认为无。 所有的值都只引用一个实例,因此将值作为可变对象(例如空列表)通常没有意义。 要获得不同的值,请改用字典理解。 -
get(key[, default])如果键在字典中,则返回键的值,否则返回默认值。 如果未给出默认值,则默认为 None,因此此方法永远不会引发 KeyError。
>>> la = [\'one\', \'two\', \'three\']
>>> a = dict.fromkeys(la, 0)
{\'one\': 0, \'two\': 0, \'three\': 0}
>>> a.get("one")
0
>>> a.get("four", "Not Find")
\'Not Find\'
-
items()返回字典项((键,值)对)的新视图。 -
keys()返回字典键的新视图。 -
values()返回字典值的新视图。
一个dict.values()视图和另一个视图之间的相等比较将始终返回 False。 这也适用于将dict.values()与其自身进行比较时
>>> a.items()
dict_items([(\'one\', 0), (\'two\', 0), (\'three\', 0)])
>>> a.keys()
dict_keys([\'one\', \'two\', \'three\'])
>>> a.values()
dict_values([1, 2, 3])
>>> a.values() == a.values()
False
-
pop(key[, default])如果 key 在字典中,删除它并返回它的值,否则返回 default。 如果没有给出默认值并且 key 不在字典中,则会引发 KeyError。 -
popitem()从字典中删除并返回一个(key, value)对。 对按 LIFO 顺序返回。popitem()对于破坏性地迭代字典很有用,这在集合算法中经常使用。 如果字典为空,则调用popitem()会引发 KeyError。在 3.7 版更改:现在保证 LIFO 顺序。 在以前的版本中,popitem()将返回任意键/值对。
>>> a.pop("one", "Not pop")
0
>>> a
{\'two\': 0, \'three\': 0}
>>> a.pop("four", "Not pop")
\'Not pop\'
>>> a.popitem()
(\'three\', 0)
-
在字典的键上返回一个反向迭代器。 这是reversed(d.keys())` 的快捷方式。 -
setdefault(key[, default])如果key在字典中,则返回它的值。 如果没有,插入值为default的键并返回默认值。default默认为None。 -
update([other])用来自other的key/value对更新字典,覆盖现有的键。 返回None。update()接受另一个字典对象或键/值对的可迭代对象(作为元组或其他长度为 2 的可迭代对象)。 如果指定了关键字参数,则字典将使用这些键/值对进行更新:d.update(red=1, blue=2)。
>>> a = {"one": 1, "two": 2, "three": 3}
>>> [x for x in reversed(a)]
[\'three\', \'two\', \'one\']
>>> a.setdefault("four", 4)
4
>>> a.update({\'four\' : 40, \'five\' : 5})
{\'one\': 1, \'two\': 2, \'three\': 3, \'four\': 40, \'five\': 5}
>>> a.update(four=4)
{\'one\': 1, \'two\': 2, \'three\': 3, \'four\': 4, \'five\': 5}
- 字典的合并
>>> a = {"fyz":"dalao", "yjk":"wuqing"}
>>> b = {"xhr":"niubi", "zc":"laji"}
>>> {**a, **b}
{\'fyz\': \'dalao\', \'yjk\': \'wuqing\', \'xhr\': \'niubi\', \'zc\': \'laji\'}
- 字典打包
>>> def dpackage(**params):
print("有%d个参数"%len(params))
print(params)
>>> dpackage(a = 1, b = 2, c = 3)
有3个参数
{\'a\': 1, \'b\': 2, \'c\': 3}
>>> a = {"one": 1, "two": 2, "three": 3}
>>> dpackage(**a)
有3个参数
{\'one\': 1, \'two\': 2, \'three\': 3}
-
d | other使用d和other的合并键和值创建一个新字典,这两者都必须是字典。当d和其他共享键时,其他的值优先。版本3.9中的新功能。 -
d |=other使用other的键和值更新字典d,这些键和值可以是key/value对的mapping或iterable。当d和其他共享键时,其他的值优先。版本3.9中的新功能。
>>> a = {"one": 1, "two": 2, "three": 3}
>>> a | {"three" : 30, "four" : 4}
{\'one\': 1, \'two\': 2, \'three\': 30, \'four\': 4}
>>> a |= {"three" : 30, "four" : 4}
>>> a
{\'one\': 1, \'two\': 2, \'three\': 30, \'four\': 4}