如何在python中比较两个字典

时间:2018-10-14 02:51:39

标签: python dictionary

我想比较两个字典并找到匹配的值

RFQDict字典是这种格式

'9905-01-485-2102': {'Add_method': 'Dibbs Extract', 'NSN': '9905-01-485-2102', 'Nomenclature': 'REFLECTOR, INDICATIN', 'RFQ_Date': '10-2-2018', 'RFQ_QTY': ' 4200', 'Time': '2018-10-13 22:19:01'}

AwardsDict字典是这种格式

'W91WAW07C0023': {'Awarddate': '08-13-2007', 'Awardid': 'W91WAW07C0023', 'Cage': '7A293', 'Deliverynumber': '»', 'NSN': '0001', 'Nomenclature': 'INSTITUTE FOR  ANALYSES', 'PR': '0014447158', 'Price': 'See Award Doc', 'Time': '2018-05-17 17:41:54'}

我想比较NSN值以查看是否存在匹配项

我使用此脚本获取了两个字典

RFQDict = {}
AwardsDict = {}

# Fetch RFQ 

RFQref = db.reference('TestRFQ')

snapshot = RFQref.get()
for key, val in snapshot.items():
    RFQDict[key] = val
    print('{0} => {1}'.format(key, val))


Awardsref = db.reference('DibbsAwards')

dsnapshot = Awardsref.get()
for key, val in dsnapshot.items():
    AwardsDict[key] = val
    print('{0} => {1}'.format(key, val))

print(RFQDict)
print(AwardsDict)

每个词典包含数千个键值。 如何比较NSN值?

2 个答案:

答案 0 :(得分:1)

3.47 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.82 ms ± 149 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

答案 1 :(得分:1)

这是您问题的简化版本。您可以按AwardsDictNSN“值”字典分组,然后进行比较和匹配。

RFQDict = {
    'a': {'Add_method': 'D1', 'NSN': '9905'},
    'b': {'Add_method': 'D2', 'NSN': '9906'},
    'c': {'Add_method': 'D3', 'NSN': '9907'},
    'd': {'Add_method': 'D4', 'NSN': '9908'}
}
AwardsDict = {
    'W21': {'Awarddate': '08-13-2007', 'Awardid': '1', 'NSN': '9906'},
    'W22': {'Awarddate': '08-14-2007', 'Awardid': '2', 'NSN': '9905'},
    'W23': {'Awarddate': '08-15-2007', 'Awardid': '3', 'NSN': '9908'},
    'W24': {'Awarddate': '08-16-2007', 'Awardid': '4', 'NSN': '9907'},
}

# First create a new dictionary with "NSN" as keys and awards as matches 
nsn_awards = {v['NSN']: v for v in AwardsDict.values()}

# go through all values of RFQDict and find a match by
# looking up the its NSN in the nsn_awards_dict
matches = [(rfq, nsn_awards[rfq['NSN']]) for rfq in RFQDict.values()]

print(matches)

打印:

[({'NSN': '9908', 'Add_method': 'D4'}, {'NSN': '9908', 'Awardid': '3', 'Awarddate': '08-15-2007'}), ({'NSN': '9907', 'Add_method': 'D3'}, {'NSN': '9907', 'Awardid': '4
', 'Awarddate': '08-16-2007'}), ({'NSN': '9906', 'Add_method': 'D2'}, {'NSN': '9906', 'Awardid': '1', 'Awarddate': '08-13-2007'}), ({'NSN': '9905', 'Add_method': 'D1'}
, {'NSN': '9905', 'Awardid': '2', 'Awarddate': '08-14-2007'})]

请注意,这为我们提供了O(n)解决方案,但需要权衡的是我们要创建的字典的空间。

如果AwardsDict中有可能存在不匹配的NSN,则可以在创建匹配项之前执行简单的if nsn in nsn_awards检查。为了更好地解释,这是一个没有列表理解的版本:

matches = []
for rfq in RFQDict.values():
    if rfq['NSN'] in nsn_awards: # only append if there's a match
        nsn = rfq['NSN']
        matches.append((rfq, nsn_awards[nsn]))