a = {2: 4, 3: 2, 5: 1, 7: 1}
键代表素数;这些值代表计数器。我想通过遍历字典键*值并对总数求和来计算你得到的数字。最Pythonic的方法是什么?
>>> [k*v for k,v in a.items()]
[8, 6, 5, 7]
但
>>> sum(k*v for k,v in a.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable