Python 字典 fromkeys() 方法
定义和用法
fromkeys() 方法返回具有指定键和值的字典。
实例
例子 1
创建拥有 3 个键的字典,值均为 0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
例子 2
与上例相同,但未指定值:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)
语法
dict.fromkeys(keys, value)
参数
| 参数 | 描述 |
|---|---|
| keys | 必需。指定新字典键的可迭代对象。 |
| value | 可选。所有键的值。默认值是 None。 |