如何对字典中的所有值求和? [英] How to sum all the values in a dictionary?

查看:47
本文介绍了如何对字典中的所有值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字典,其中的键映射到整数,例如:

Let's say I have a dictionary in which the keys map to integers like:

d = {'key1': 1,'key2': 14,'key3': 47}

是否有一种语法简约的方法来返回 d 中的值的总和——即62 在这种情况下?

Is there a syntactically minimalistic way to return the sum of the values in d—i.e. 62 in this case?

推荐答案

如您所料:

sum(d.values())

这篇关于如何对字典中的所有值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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