代码点读机 / Python / fromkeys

fromkeys Python · 字典方法

用一组键快速创建值都相同的字典。

怎么用

dict.fromkeys(键序列, 默认值=None)

小例子

print(dict.fromkeys(['a', 'b'], 0))

运行结果

{'a': 0, 'b': 0}

官方文档:https://docs.python.org/zh-cn/3/library/stdtypes.html#typesmapping

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条