返回字典Python中所有值的总和

And*_*ams 2 python dictionary sum count

需要帮助制作函数sumcounts(D)其中D是一个以数字作为值的字典,返回所有值的总和.示例输出应如下所示:

>>> sumcounts({"a":2.5, "b":7.5, "c":100})
110.0
>>> sumcounts({ })
0
>>> sumcounts(strcount("a a a b"))
4
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 8

它已经存在:

sum(d.values())
Run Code Online (Sandbox Code Playgroud)

或许那样:

def sumcount(d):
    return sum(d.values())
Run Code Online (Sandbox Code Playgroud)