将复数词典中的值与另一个词典一起检查,然后将其保存到第三本词典中

时间:2019-05-22 22:42:22

标签: python dictionary

词典的输入词典是dict1和dict2。

dict1 = {company1:[{'age':27,'weight':200,'name':'john'},{'age':23,'weight':180,'name':'peter'}],
         company2:[{'age':30,'weight':190,'name':'sam'},{'age':32,'weight':210,'name':'clove'},{'age':21,'weight':170,'name':'steve'}],
         company3:[{'age':36,'weight':175,'name':'shaun'},{'age':40,'weight':205,'name':'dany'},{'age':25,'weight':160,'name':'mark'}]
         company4:[{'age':36,'weight':155,'name':'lina'},{'age':40,'weight':215,'name':'sammy'},{'age':25,'weight':190,'name':'matt'}]
        }

dict2 = {company2:[{'age':30},{'age':45},{'age':52}],
         company4:[{'age':43},{'age':67},{'age':22},{'age':34},{'age':42}]
        }

我正在尝试编写一种逻辑,以便我可以检查dict2中每个复合键的内键(“ age”)是否存在于同一公司键dict1中,即使内键“ age”的一个值与内键(“相同公司密钥的dict1中的“年龄”),然后将其保存到第三本词典中。请检查以下示例

示例

company2:[{'age':30}]

匹配
company2:[{'age':30,'weight':190,'name':'sam'}, ...]

我还想将key2:未在dict2中对应的dict1值保存到dict3,如下面的示例所示,company1密钥在dict2中未对应。

示例

company1:[{'age':27,'weight':200,'name':'john'},{'age':23,'weight':180,'name':'peter'}]

company3:[{'age':36,'weight':175,'name':'shaun'},{'age':40,'weight':205,'name':'dany'},{'age':25,'weight':160,'name':'mark'}]

预期输出:

dict3 = {company1:[{'age':27,'weight':200,'name':'john'},{'age':23,'weight':180,'name':'peter'}],
         company2:[{'age':30,'weight':190,'name':'sam'},{'age':32,'weight':210,'name':'clove'},{'age':21,'weight':170,'name':'steve'}]
         company3:[{'age':36,'weight':175,'name':'shaun'},{'age':40,'weight':205,'name':'dany'},{'age':25,'weight':160,'name':'mark'}]}

请原谅我!

1 个答案:

答案 0 :(得分:1)

可能更简洁地使用其他方法可以更好地完成此解决方案。但是,它可以达到预期的结果。

from pprint import pprint

dict3 = dict()

dict1 = {'company1':[{'age':27,'weight':200,'name':'john'},{'age':23,'weight':180,'name':'peter'}],
         'company2':[{'age':30,'weight':190,'name':'sam'},{'age':32,'weight':210,'name':'clove'},{'age':21,'weight':170,'name':'steve'}],
         'company3':[{'age':36,'weight':175,'name':'shaun'},{'age':40,'weight':205,'name':'dany'},{'age':25,'weight':160,'name':'mark'}],
         'company4':[{'age':36,'weight':155,'name':'lina'},{'age':40,'weight':215,'name':'sammy'},{'age':25,'weight':190,'name':'matt'}]
        }

dict2 = {'company2':[{'age':30},{'age':45},{'age':52}],
         'company4':[{'age':43},{'age':67},{'age':22},{'age':34},{'age':42}]
        }

for company, array in dict1.items():
    if company not in dict2:
        dict3[company] = array
    else:
        # all the ages for this company in dict1
        ages = set(map(lambda x: x['age'], array))

        for dictref in dict2[company]:
            if dictref['age'] in ages:
                dict3[company] = array
                break
pprint(dict3)

输出原为

{'company1': [{'age': 27, 'name': 'john', 'weight': 200},
              {'age': 23, 'name': 'peter', 'weight': 180}],
 'company2': [{'age': 30, 'name': 'sam', 'weight': 190},
              {'age': 32, 'name': 'clove', 'weight': 210},
              {'age': 21, 'name': 'steve', 'weight': 170}],
 'company3': [{'age': 36, 'name': 'shaun', 'weight': 175},
              {'age': 40, 'name': 'dany', 'weight': 205},
              {'age': 25, 'name': 'mark', 'weight': 160}]}