简体   繁体   English

如何将字典列表与 python 中的每个键进行比较?

[英]how to compare list of dict with each key in python?

list1 = [
    {'A':'a','B':'b','C':'c'},
    {'A':'aa','B':'bb','C':'cc'},
    {'A':'aaa','B':'bbb','C':'ccc'}
    ]

list2 =  [
    {'A':'a','B':'b','C':'c'},
    {'A':'aa','B':'bb','C':'cc'},
    {'A':'aaa','B':'bbb','C':'ccc'}
    ]

I have 2 such list of dict (ex), I want to compare each key of both lists, means A of dict1 1st list with A of dict1 2nd list, A of dict2 of list1 to A of dict2 of list2 similarly I have to check all the keys, but my expected output is我有 2 个这样的 dict 列表(ex),我想比较两个列表的每个键,意味着 dict1 第一个列表的 A 与 dict1 第二个列表的 A,list1 的 dict2 的 A 到 list2 的 dict2 的 A 同样我必须检查所有的钥匙,但我预期的 output 是

{'A':True, 'B':True, 'C':True} Means if all the A match with each other from both the dict it will return true and even If one do not match it will written as false {'A':True, 'B':True, 'C':True}如果两个字典中的所有 A 相互匹配,它将返回 true,即使一个不匹配,它也会写为 false

( ex in dict2 of list 1 if value of say 'B' is 'bb' if that do not match with dict2 of list 2 then B will be false if all all other B are matching in other dict (例如,在列表 1 的 dict2 中,如果说 'B' 的值为 'bb',如果与列表 2 的 dict2 不匹配,那么如果所有其他 B 在其他 dict 中匹配,则 B 将为假

You can get all the keys for all dicts from both lists, and then check the set of values are the same from each list using a dict comprehension.您可以从两个列表中获取所有字典的所有键,然后使用字典推导检查每个列表中的值集是否相同。

keys = set(k for d in list1 + list2 for k in d.keys())
d_combined = {k:set(d[k] for d in list1) == set(d[k] for d in list2) for k in keys}

Ouput:输出:

{'A': True, 'C': True, 'B': True}

But if the order of the elements in the two lists is important then this won't be sufficient.但如果两个列表中元素的顺序很重要,那么这还不够。

Looping through the keys of dict in both list So, try this code;遍历两个列表中 dict 的键 所以,试试这个代码;

Solution 1: (If order of keys is important):解决方案1:(如果键的顺序很重要):

dct = {}
for l1,l2 in zip(list1,list2):
    for k1,k2 in zip(l1.keys(),l2.keys()):
        if k1 == k2:
            if k1 in dct.keys():
                dct[k1] += 1
            else:
                dct[k1] = 1
for key,value in dct.items():
    if value != len(list1):
        dct[key] = False
    else:
        dct[key] = True

Solution 2: (If order of keys is not important):解决方案2:(如果键的顺序不重要):

dct = {}
for l1,l2 in zip(list1,list2):
    for k1 in l1.keys():
        if k1 in l2.keys():
            if k1 in dct.keys():
                dct[k1] += 1
            else:
                dct[k1] = 1
for key,value in dct.items():
    if value != len(list1):
        dct[key] = False
    else:
        dct[key] = True

Hope this Helps...希望这可以帮助...

By using pandas :通过使用pandas

import pandas as pd

list1 = [
    {'A':'a','B':'b','C':'c'},
    {'A':'aa','B':'bb','C':'cc'},
    {'A':'aaa','B':'bbb','C':'ccc'}
    ]

list2 =  [
    {'A':'a','B':'b','C':'c'},
    {'A':'aa','B':'bb','C':'cc'},
    {'A':'aaa','B':'bbb','C':'ccc'}
    ]

df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list1)

output={}
for i in df1.columns:
    output[i] = df1[i].equals(df2[i])

        
print(output)

Output: Output:

{'A': True, 'B': True, 'C': True}

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

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