Python/ref dictionary fromkeys
来自菜鸟教程
<languages />
Python字典fromkeys()方法
例
创建具有3个键的字典,所有键的值均为0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
定义和用法
The
fromkeys()
方法返回具有指定键和指定值的字典。
句法
dict.fromkeys(keys, value)
参数值
| 参数 | 描述 |
|---|---|
| keys | 需要。指定新字典键的迭代器 |
| value | 可选的。所有键的值。默认值为无 |
更多例子
例
与上述示例相同,但未指定值:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)