有没有办法在python中从列表中获取键,将字典值初始化为0?

Nav*_*jum 0 python dictionary

我有一个列表用作字典的键,并且对应于键的每个值都将初始化为0。

Rah*_*K P 9

你可以做 dict.fromkeys

In [34]: dict.fromkeys(range(5),0)
Out[34]: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
In [35]: dict.fromkeys(['a','b','c'],0)
Out[35]: {'a': 0, 'b': 0, 'c': 0}
Run Code Online (Sandbox Code Playgroud)

  • 这有帮助。但是,如果将单词列表作为键呢? (2认同)