繁体   English   中英

如何将一个动态字典列表与另一个 static 字典列表进行比较以从动态列表中查找缺失的键值对?

[英]How to compare one dynamic list of dicts to another static list of dicts to find missing key value pair from dynamic list?

我有两个字典列表如下:

 l1 = [{'CreatedOn': datetime.datetime(2022, 7, 12, 12, 12, 46, 993000),DocumentCategory': '0.1 Admission Form'},  CreatedOn': datetime.datetime(2022, 7, 12, 12, 13, 53, 900000),DocumentCategory': '0.2 Doctor Notes'},CreatedOn': datetime.datetime(2022, 7, 12, 12, 14, 8, 817000), DocumentCategory': '0.3 Nurse Notes'},CreatedOn': datetime.datetime(2022, 7, 12, 12, 14, 42, 827000), DocumentCategory': '0.4 Lab Reports'}, CreatedOn': datetime.datetime(2022, 7, 12, 12, 15, 31, 57000), DocumentCategory': '0.5 Medical History'}, {'CreatedOn': datetime.datetime(2022, 7, 12, 12, 16, 14, 603000), 'DocumentCategory': '0.6 Medical Reports'}, CreatedOn': datetime.datetime(2022, 7, 13, 15, 2, 36, 643000),DocumentCategory': '0.7 Radiology'}, CreatedOn': datetime.datetime(2022, 7, 13, 15, 2, 54, 947000),  DocumentCategory': '0.8 Discharge Notes'}]

 l2 = [{'CreatedOn': datetime.datetime(2022, 7, 13, 10, 52, 27, 260000), DocumentCategory': '0.1 Admission Form'},CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 103000),DocumentCategory': '0.2 Doctor Notes'}, CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 460000), DocumentCategory': '0.3 Nurse Notes'},CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 823000), DocumentCategory': '0.4 Lab Reports'},CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 990000),DocumentCategory': '0.5 Medical History'},  {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 55, 240000), 'DocumentCategory': '0.6 Medical Reports'}] 

我只想得到丢失的一对,在上述情况下应该是:

missing = [{'CreatedOn': datetime.datetime(2022, 7, 13, 15, 2, 36, 643000), DocumentCategory': '0.7 Radiology'},{'CreatedOn': datetime.datetime(2022, 7, 13, 15, 2, 54, 947000), 'DocumentCategory': '0.8 Discharge Notes'}]

这是我尝试过的:

missing = [x for x in l2 if x not in l1]

但是,这只是给我配对。

[{'CreatedOn': datetime.datetime(2022, 7, 13, 10, 52, 27, 260000), 'DocumentCategory': '0.1 Admission Form'}, {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 103000), 'DocumentCategory': '0.2 Doctor Notes'}, {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 460000), 'DocumentCategory': '0.3 Nurse Notes'}, {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 823000), 'DocumentCategory': '0.4 Lab Reports'}, {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 54, 990000), 'DocumentCategory': '0.5 Medical History'}, {'CreatedOn': datetime.datetime(2022, 7, 13, 10, 53, 55, 240000), 'DocumentCategory': '0.6 Medical Reports'}]

由于许多拼写错误导致语法错误,我无法确认您的特定示例,但是您使用的方法应该可以工作。

使用一个基本示例:

l1 = [{'a':1,'b':2}, {'a':2,'b':3}]
l2 = [{'a':1,'b':2}, {'a':9,'b':10}]

missing = [x for x in l2 if x not in l1]

返回[{'a': 9, 'b': 10}]

我们可以使用 for 循环。 列表中的 dict 将被解包,因此我们可以迭代 dict 的项目。

l1 = [{'name': 'Jon', 'Company': 'XYZ Ltd', 'age':45, 'LoggedIn': 'Yes'}]
l2 = [{'name': 'Jon', 'age':45}]
res = {}
for pair in l1[0].items():
    if pair[0] not in l2[0]:
        res.update({pair})
print(res) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM