python 3 中字典值的总和

sum of dictionary values in python 3

我有以下词典:

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?

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