我有两个清单,我需要比较和输出的差异报告
我需要:
要处理:
new_list = [
{'datetime': '2018-08-01', 'evar1': 'newRecord', 'event16': '100', 'event15': '200'},
{'datetime': '2018-08-02', 'evar1': 'duplicateRecord', 'event16': '10', 'event15': '20'},
{'datetime': '2018-08-03', 'evar1': 'diffEvent', 'event16': '15', 'event15': '25'}
]
old_list = [
{'datetime': '2018-08-02', 'evar1': 'duplicateRecord', 'event16': '10', 'event15': '20'},
{'datetime': '2018-08-03', 'evar1': 'diffEvent', 'event16': '10', 'event15': '25'}
]
结果应如下所示:
updated_list = [
{'datetime': '2018-08-01', 'evar1': 'newRecord', 'evar3': 'site', 'event16': '100', 'event15': '200'},
{'datetime': '2018-08-03', 'evar1': 'diffEvent', 'evar3': 'site', 'event16': '5', 'event15': '25'}
]
我试过这个:
updated_list = []
for new_item in new_list:
for old_item in old_list:
for key, value in new_item.iteritems():
# If values don't match, subtract old_list value from new_list values and append the diff
if any(ko == key for ko, vo in old_item.iteritems()):
ko, vo = [(ko, vo) for (ko, vo) in old_item.iteritems() if ko == key][0]
if vo != value:
new_value = value - vo
new_item.update({ko: new_value})
updated_list.append(new_item)
else:
# If record does not exist in old_list, append the new record
updated_list.append(new_item)
真是个令人困惑的任务!这是我的解决方案(注释应该解释方法)。你知道吗
相关问题 更多 >
编程相关推荐