Skip to main content
 首页 » 编程设计

python之python 3中字典值的总和

2025年05月04日13小虾米

我有以下字典:

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

如何找到键的总和?在 Python 2 中,我可以执行以下操作:

sum(dic.values()) 

除了这个还有别的办法吗?我尝试使用 dic.values() 并找到该列表的总和,但它太长了。

请您参考如下方法:

sum(dic.values()) 

Is there any other way than this?

当然。无穷多。但你为什么要问?这是正确的做法。