Python:对每个字典值执行一个操作 [英] Python: Perform an operation on each dictionary value
本文介绍了Python:对每个字典值执行一个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python 2.6中,我想对每个字典值执行一个操作,例如,我想为每个字典值乘以2。如何为此任务减少代码?
In python 2.6 I want to perform an operation on each dictionary value, for example, I want to multiply by 2 for each of them. How to code less for this task?
推荐答案
# A nice one liner (edited to remove square brackets)
my_dict.update((x, y*2) for x, y in my_dict.items())
这篇关于Python:对每个字典值执行一个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文