Python:替换字典中的所有值 [英] Python: Replace All Values in a Dictionary

查看:43
本文介绍了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:替换字典中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆