我有两个字典列表:
old_data = [{'company': 'Amazon', 'logged_in': '2019-01-20'},
{'company': 'Facebook', 'logged_in': '2019-04-20'},
{'company': 'Google', 'logged_in': '2019-04-20'}]
new_data = [{'company': 'Amazon', 'logged_in': '2019-01-26'},
{'company': 'Facebook', 'logged_in': '2019-04-12'},
{'company': 'LinkedIn', 'logged_in': '2019-04-20'}]
我想将new_data过滤为:
预期结果:
[{'company': 'Amazon', 'logged_in': '2019-01-26'},
{'company': 'LinkedIn', 'logged_in': '2019-04-20'}]
答案 0 :(得分:0)
filter_new_data = []
for old_dict, new_dict in zip(old_data, new_data):
if new_dict['company'] not in [d['company'] for d in old_data]:
filter_new_data.append(new_dict)
elif new_dict['company'] == old_dict['company'] and new_dict['logged_in'] > old_dict['logged_in']:
filter_new_data.append(new_dict)