简体   繁体   English

通过比较两个字典的值来创建第三个字典

[英]Create third dictionary by comparing the values of two dictionaries

I have Two dictionaries like below我有两个字典,如下所示

dic1 = {'1': 'india','2': 'america','3': 'china'}
dic2 = {'A1':'india','A2':'india' ,'A3':'america','A4':'india' ,'A5': 'china','A6': 'india','A7': 'america' }

I want to create new dictionary by comparing the values and store the values in third dictionary as below我想通过比较值来创建新字典并将值存储在第三个字典中,如下所示

 dic3 =  {'1': [A1,A2,A4,A6], '2': [A3,A7] ,'3': [A5] }

i tried with following code but its not giving expected results我尝试使用以下代码,但没有给出预期的结果

dict3={}
for i in dic1:
    for j in dic2:
        if i == dic2[j]:
            dict3[j]=dic1[i]

print(dict3)

One way using collections.defaultdict :使用collections.defaultdict一种方法:

from collections import defaultdict

tmp = {v: k for k, v in dic1.items()}
dic3 = defaultdict(list)

for k, v in dic2.items():
    dic3[tmp[v]].append(k)
dic3 = dict(dic3)

print(dic3)

Output: Output:

{'1': ['A1', 'A2', 'A4', 'A6'], '2': ['A3', 'A7'], '3': ['A5']}

Try out this code:试试这个代码:

dic1 = {'1': 'india','2': 'america','3': 'china'}
dic2 = {'A1':'india','A2':'india' ,'A3':'america','A4':'india' ,'A5': 'china','A6': 'india','A7': 'america' }
dic3 = {}

# Iterate through each item pair in dic1
for tup in dic1.items():
    # Iterate through each of dic2's keys
    for key in dic2.keys():
        # Checks if dic2's value equals dic1's value
        if dic2[key] == tup[1]:
            # If so, either creates a new key or adds to an existing key in dic3
            if tup[0] in dic3:
                dic3[tup[0]].append(key)
            else:
                dic3[tup[0]] = [key]
                
print(dic3)

This iterates through each ITEM PAIR in dic1, checks if dic2's value is equal to dic1's value, and if so appends dic2's key to the value of dic1's key inside of dic3.这将遍历 dic1 中的每个 ITEM PAIR,检查 dic2 的值是否等于 dic1 的值,如果是,则将 dic2 的键附加到 dic3 内 dic1 的键的值。 Complicated stuff.复杂的东西。

If you want your result to be this:如果你希望你的结果是这样的:

dic3 =  {'1': [A1,A2,A4,A6], '2': [A3,A7] ,'3': [A5] }

This will work, I have tried to keep your original solution with some modifications so it will give you desired output:这将起作用,我试图通过一些修改来保留您的原始解决方案,这样它就会为您提供所需的 output:

dic1 = {"1": "india", "2": "america", "3": "china"}
dic2 = {
    "A1": "india",
    "A2": "india",
    "A3": "america",
    "A4": "india",
    "A5": "china",
    "A6": "india",
    "A7": "america",
}

dic3 = {}

for i in dic1:  # iterate through keys of dic1
    lst = []  # list of all the keys of dic2 which have same value as i
    for j in dic2:  # iterate through keys of dic2
        if dic1[i] == dic2[j]:  # if value of dic1 is equal to value of dic2
            lst.append(j)  # add that key to the lst list
    dic3[i] = lst  # at last after lst is completed add it to dic3 with i as key.

print(dic3)

This is the output:这是 output:

{'1': ['A1', 'A2', 'A4', 'A6'], '2': ['A3', 'A7'], '3': ['A5']}

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

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