简体   繁体   English

比较 Python 中的两个字典列表。 返回不匹配

[英]Compare two lists of dictionaries in Python. Return non match

I have two lists of dictionaries like below:我有两个字典列表,如下所示:

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

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

I want to compare the incidentId with each other and return the non_match.我想将incidentId相互比较并返回non_match。 This is my code now, but it returns just the two items, because it is comparing the whole item instead of only the incidentId .这是我现在的代码,但它只返回两个项目,因为它正在比较整个项目而不是仅仅incidentId What's a good way to do this?有什么好方法可以做到这一点?

non_match = []
for i in incidents:
    if i not in incidents_db:
        non_match.append(i)
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'}]

If you only want to compare two specific items from the dictionary.如果您只想比较字典中的两个特定项目。 Do this:做这个:

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)

if you want to compare only " incidentId"如果您只想比较“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)
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)

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

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