Python dict.fromkeys() 方法
描述
用可迭代对象中的元素作为键创建新字典,所有键的值都是 value。
语法
dict.fromkeys(iterable[, value])
参数
| 参数 | 说明 |
|---|---|
iterable | 键 |
value | 统一的值,默认 None |
返回值
新字典
实例
print(dict.fromkeys(["a", "b"], 0))
print(list(dict.fromkeys([3, 1, 3, 2]))) # 保序去重以上实例输出结果为:
{'a': 0, 'b': 0}
[3, 1, 2]