Let's say I have a list (or other iterable object) ['a','b','c','d']
. I have a dict
X
. I can sum them up in the naive way:
s = 0
for k in ['a','b','c','d']:
s += X[k]
But is there a more pythonic way?
You can do:
s = sum(X[k] for k in ['a','b','c','d'])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句