简体   繁体   English

python 3中字典值的总和

[英]sum of dictionary values in python 3

I have the following dictionary: 我有以下字典:

dic= {'I-setosa': 8, 'I-versicolor': 2, 'I-virginica': 0}

How can i find the sum of the keys? 如何找到密钥的总和? In Python 2 I could do the following: Python 2我可以执行以下操作:

sum(dic.values())

Is there any other way than this? 除此之外,还有其他方法吗? I tried using dic.values() and find the sum of that list, but it is too lengthy. 我尝试使用dic.values()并找到该列表的总和,但是它太长了。

 sum(dic.values()) 

Is there any other way than this? 除此之外,还有其他方法吗?

Sure. 当然。 Infinitely many. 无限多个。 But why are you asking? 但是你为什么要问? That's the right way to do it. 这是做正确方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM