本文介绍了Python:替换字典中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以用整数1替换字典中的所有值,而不论其值是什么?
Is it possible to replace all values in a dictionary, regardless of value, with the integer 1?
谢谢!
推荐答案
当然,您可以执行以下操作:
Sure, you can do something like:
d = {x: 1 for x in d}
这将创建一个新字典 d
,该字典将 d
中的每个键(旧键)映射到 1
.
That creates a new dictionary d
that maps every key in d
(the old one) to 1
.
这篇关于Python:替换字典中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!