Python 如何对字典中的所有值求和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4880960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sum all the values in a dictionary?
提问by nedblorf
Let's say I have a dictionary in which the keys map to integers like:
假设我有一个字典,其中的键映射到整数,例如:
d = {'key1': 1,'key2': 14,'key3': 47}
Is there a syntactically minimalistic way to return the sum of the values in d—i.e. 62in this case?
是否有返回值的总和在语法上简约的方式d-即62在这种情况下?
采纳答案by phihag
As you'd expect:
正如你所期望的:
sum(d.values())
回答by vz0
Sure there is. Here is a way to sum the values of a dictionary.
当然有。这是一种对字典值求和的方法。
>>> d = {'key1':1,'key2':14,'key3':47}
>>> sum(d.values())
62
回答by martineau
In Python 2 you can avoid making a temporary copy of all the values by using the itervalues()dictionary method, which returns an iterator of the dictionary's keys:
在 Python 2 中,您可以使用itervalues()字典方法避免制作所有值的临时副本,该方法返回字典键的迭代器:
sum(d.itervalues())
In Python 3 you can just use d.values()because that method was changed to do that (and itervalues()was removed since it was no longer needed).
在 Python 3 中,您可以使用,d.values()因为该方法已更改为这样做(并且itervalues()由于不再需要它而被删除)。
To make it easier to write version independent code which always iterates over the values of the dictionary's keys, a utility function can be helpful:
为了更容易编写始终迭代字典键值的版本无关代码,实用函数可能会有所帮助:
import sys
def itervalues(d):
return iter(getattr(d, ('itervalues', 'values')[sys.version_info[0]>2])())
sum(itervalues(d))
This is essentially what Benjamin Peterson's sixmodule does.
这本质上是 Benjamin Peterson 的six模块所做的。
回答by Kalyan Pendyala
d = {'key1': 1,'key2': 14,'key3': 47}
sum1 = sum(d[item] for item in d)
print(sum1)
you can do it using the for loop
你可以使用 for 循环
回答by Pratyush Raizada
I feel sum(d.values())is the most efficient way to get the sum.
我觉得sum(d.values())是获得总和的最有效方法。
You can also try the reduce function to calculate the sum along with a lambda expression:
您还可以尝试使用 reduce 函数来计算总和以及 lambda 表达式:
reduce(lambda x,y:x+y,d.values())
回答by Behlul Valiyev
sum(d.values()) - "d" -> Your dictionary Variable
sum(d.values()) - "d" -> 你的字典变量
回答by Reza
phihag's answer (and similar ones) won't work in python3.
phihag 的答案(和类似的答案)在 python3 中不起作用。
For python 3:
对于蟒蛇 3:
d = {'key1': 1,'key2': 14,'key3': 47}
sum(list(d.values()))
Update! There are complains that it doesn't work! I just attach a screenshot from my terminal. Could be some mismatch in versions etc.
更新!有人抱怨它不起作用!我只是附上我终端的屏幕截图。版本等可能有些不匹配。
回答by user12988654
You can get a generator of all the values in the dictionary, then cast it to a list and use the sum() function to get the sum of all the values.
您可以获得字典中所有值的生成器,然后将其转换为列表并使用 sum() 函数来获取所有值的总和。
Example:
例子:
c={"a":123,"b":4,"d":4,"c":-1001,"x":2002,"y":1001}
sum(list(c.values()))
回答by Rahul Patel
You could consider 'for loop' for this:
您可以为此考虑“for循环”:
d = {'data': 100, 'data2': 200, 'data3': 500}
total = 0
for i in d.values():
total += i
total = 800
总计 = 800


