【发布时间】:2017-06-01 03:16:29
【问题描述】:
dic1 = {
'a':2,
'b':3
}
dic2 = {
'a':2,
'b':3,
'c':5
}
...
为了我自己的满意,我的特定问题更多是 python 问题中的语法。假设您有一堆字典,并且想要将可能不存在的所有属性 a、b 和 c 相加……我当前的代码有点庞大,我不喜欢一堆 if elif 语句。 ..
if 'a' in dicX:
total+=dicX['a']
if 'b' in dicX:
total+=dicX['b']
if 'c' in dicX:
total+=dicX['c']
谁能推荐一种更好的单行方式来做到这一点?或者只是一种更清洁的方法?
【问题讨论】:
-
谢谢!这个的超级粉丝......
标签: python python-3.x dictionary