gpt4 book ai didi

python - 有没有一种方法可以在 python 中从列表中获取键值将字典值初始化为 0?

转载 作者:行者123 更新时间:2023-12-03 18:28:54 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to initialize a dict with keys from a list and empty value in Python?

(7 个回答)


3年前关闭。




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

最佳答案

您可以使用 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}

关于python - 有没有一种方法可以在 python 中从列表中获取键值将字典值初始化为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52410880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com