Reputation: 1659
I want to compare two dictionaries and find a matching value
RFQDict
dictionary is in this format
'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
dictionary is in this format
'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'}
I want to compare the NSN values to see if there is a match
I used this script to get the two dictionaries
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)
Each dictionary contains thousands of key values. How could I compare the NSN values ?
Upvotes: 0
Views: 755
Reputation: 12990
Here's a simplified version of your problem. You can group your AwardsDict
"value" dictionaries by NSN
and then compare and match.
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)
prints:
[({'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'})]
Note that this gives us an O(n) solution but the tradeoff is space for the dict we're creating.
If there's a possibility that you can have NSNs that don't have a match in AwardsDict
, you can perform a simple if nsn in nsn_awards
check before creating a match. To explain it better, here's a version without list comprehensions:
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]))
Upvotes: 1