2021-12-20 09:33

# 求一个特殊的python函数算法

a_l = [{'a': 1, 'b': 2, 'c': 3, 'os': "linux"}, {'a': 31, 'b': 33, 'c': 34, 'os': "windows"},{'a': 21, 'b': 23, 'c': 24, 'os': "unix"}]
b_l = [{'a': 11, 'b': 21, 'c': 31, 'os': "linux"}, {'a': 31, 'b': 53, 'c': 44, 'os': "unix"}, {'a': 21, 'b': 23, 'c': 24, 'os': "windows"}]

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答

#### 1条回答默认 最新

• chuifengde 2021-12-20 10:12
最佳回答
``````a_l = [{'a': 1, 'b': 2, 'c': 3, 'os': "linux"},
{'a': 31, 'b': 33, 'c': 34, 'os': "windows"},
{'a': 21, 'b': 23, 'c': 24, 'os': "unix"}]
b_l = [{'a': 11, 'b': 21, 'c': 31, 'os': "linux"},
{'a': 31, 'b': 53, 'c': 44, 'os': "unix"},
{'a': 21, 'b': 23, 'c': 24, 'os': "windows"}]

res = []

for i in a_l:
d = {}
for j in b_l:
if j.get('os') == i['os']:
d['a'] = j.get('a') + i['a']
d['b'] = j.get('b') + i['b']
d['c'] = j.get('c') + i['c']
d['os'] = i['os']
res.append(d)
print(res)
"""--result
[{'a': 12, 'b': 23, 'c': 34, 'os': 'linux'}, {'a': 52, 'b': 56, 'c': 58, 'os': 'windows'}, {'a': 52, 'b': 76, 'c': 68, 'os': 'unix'}]

"""

``````
评论
解决 无用
打赏 举报