【问题标题】:Compare two lists of dictionaries in Python. Return non match比较 Python 中的两个字典列表。返回不匹配
【发布时间】:2020-11-29 11:04:23
【问题描述】:

我有两个字典列表,如下所示:

incidents = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "REGISTERED"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "REGISTERED"
    }
]

incidents_db = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "PENDING"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "PENDING"
    }
]

我想比较 incidentId 并返回 non_match。这是我现在的代码,但它只返回两个项目,因为它比较整个项目而不是只比较 incidentId。有什么好的方法可以做到这一点?

non_match = []
for i in incidents:
    if i not in incidents_db:
        non_match.append(i)

【问题讨论】:

  • 如果两个字典列表中的incidentId 具有相同的值,你的代码的目的是什么?
  • 这只是一个唯一的 ID。我的数据库中有一些“事件”,我每 5 分钟就会收到新事件。我需要将这两个列表相互比较。当数据库列表中不存在事件ID时,我需要添加项目@ombk
  • 所以您实际上希望代码检查incidents 中的新实例是否还没有在incidents_db 中并将其添加到那里?
  • 是的。但有时incidentState 与上面的示例不同。但我只关心incidentId。如果来自incidents 的实例已经在incidents_db 中具有相同的ID,我不需要添加项目@ombk
  • 检查我发送的代码,它解决了你的问题

标签: python dictionary compare


【解决方案1】:
incidents = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "REGISTERED"
    },
    {
        "incidentId": "RWS03_947565",
        "incidentState": "REGISTERED"
    },
        {
        "incidentId": "RWS02_947565",
        "incidentState": "REGISTERED"
    },
        {
        "incidentId": "RWS05_947565",
        "incidentState": "REGISTERED"
    }
]

incidents_db = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "PENDING"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "PENDING"
    }
]
for i in incidents:
    c = 0
    for j in incidents_db:
        if j["incidentId"] == i["incidentId"]:
            c+=1
    if c==0:
        incidents_db.append(i)
#output
incidents_db

[{'incidentId': 'RWS03_949563', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_949565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_947565', 'incidentState': 'REGISTERED'},
 {'incidentId': 'RWS02_947565', 'incidentState': 'REGISTERED'},
 {'incidentId': 'RWS05_947565', 'incidentState': 'REGISTERED'}]

for i in incidents:
    c = 0
    for j in incidents_db:
        if j["incidentId"] == i["incidentId"]:
            c+=1
    if c==0:
        i["incidentState"] = "PENDING"
        incidents_db.append(i)
#output

[{'incidentId': 'RWS03_949563', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_949565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_947565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS02_947565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS05_947565', 'incidentState': 'PENDING'}]

【讨论】:

  • 输出的最后三个示例是修改示例我更改了RWS## 或其中的代码47 而不是49
  • @LucasScheepers 欢迎您,下次明确提出您的问题:p
  • 您能否解释一下c+=1if c==0 会发生什么。没看懂
  • @LucasScheepers 如果它们匹配,c 接收 +1,您可以添加 c+=1 a break ,但我不知道您在 python 中的级别,所以我尽可能简单。如果 c 增加 1,则意味着不应将事件中的值添加到数据库中。如果c==0 这意味着你没有增加任何东西。因此它是一个很好的例子
  • 啊啊啊当然。并且 c 将 incidents 循环中的每个项目重置为零。我懂了。再次感谢
【解决方案2】:
non_match = []
for a,b in zip(incidents, incidents_db):
    if a.get("incidentId") != b.get("incidentId"):
        non_match.append(a.get("incidentId"))
print(non_match)

【讨论】:

  • 因此,如果来自incidents 的字典列表比incidents_db 列表长,它只会跳过其余项目。我不想要那个:(
【解决方案3】:

如果你只想比较“incidentId”

non_match = []
for i in range(0,min(len(incidents),len(incidents_db))):
    if incidents[i].get("incidentId") != incidents_db[i].get("incidentId"):
        non_match.append(incidents[i])
print(non_match)

【讨论】:

  • 嗯没关系。当字典列表的长度不同时,我得到一个索引超出范围的错误:(
  • @LucasScheepers 此代码以并行方式比较两个字典中的元素。这就是您收到此错误的原因
  • 是的,我知道,您知道另一种解决方案吗? @ombk
  • 我编辑了 .与小列表 eventId 比较
【解决方案4】:

如果您只想比较字典中的两个特定项目。这样做:

non_match = []

for incident_dict in incidents:
    for db_dict in incidents_db:
        for key, value in incident_dict.items():
            if not db_dict.get(key, False):
                non_match.append(value)

【讨论】:

  • 我不能这样做,因为incidents 是一个列表
  • @LucasScheepers 是的,我刚刚看到了,但我可以编辑代码以使用列表,我现在正在这样做
  • 列表的长度永远不会相同。但仍然需要比较它们
  • @LucasScheepers 我只有一个问题,您想将事件的第一个字典元素与 event_db 字典的所有元素进行比较,还是仅比较相应的索引。
  • @LucasScheepers 我再次编辑了代码以使用可变长度的列表。你能检查一下这是否是你想要做的。
猜你喜欢
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多