Issue
I have an input dictionary like this:
dict1= {'AM': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR', 'tr'],
'BR': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR', 'tr']}
Two other dictionaries in which I want to search
dict2={'AM':{'pq','rs','tv'},'BR':{'MN','OP','QR'}}
dict3={'AM':{'tq','rs','tv'},'BR':{'tN','tP','tr'}}
Expected Output
{'AM-tv': 1,
'AM-rs': 1,
'AM-pq': 1,
'AM-MN': 0,
'AM-tN': 0,
'AM-tq': 1,
'AM-OP': 0,
'AM-tP': 0,
'AM-QR': 0,
'AM-tr': 0,
'BR-tv': 0,
'BR-rs': 0,
'BR-pq': 0,
'BR-MN': 1,
'BR-tN': 1,
'BR-tq': 0,
'BR-OP': 1,
'BR-tP': 1,
'BR-QR': 1,
'BR-tr': 1}
In output, there is a need to generate pairs from dict1. if the values of dict1 keys are present in values of dict2 or dict3 keys then its value will be 1 in output. if values are not present then its value will be 0 in new dict. is there anyway to do it?
Solution
One option is to do a set membership check for each element in the lists in dict1
as we traverse dict1
:
out = {}
for k, lst in dict1.items():
s = dict2[k] | dict3[k]
for v in lst:
out[f"{k}-{v}"] = (v in s) * 1
We can also write the same code as a dict comprehension (but it's slower than the other option since we do set union len(lst)
times instead of once as in the loop):
out = {f"{k}-{v}": (v in dict2[k] | dict3[k]) * 1 for k, lst in dict1.items() for v in lst}
Output:
{'AM-tv': 1,
'AM-rs': 1,
'AM-pq': 1,
'AM-MN': 0,
'AM-tN': 0,
'AM-tq': 1,
'AM-OP': 0,
'AM-tP': 0,
'AM-QR': 0,
'AM-tr': 0,
'BR-tv': 0,
'BR-rs': 0,
'BR-pq': 0,
'BR-MN': 1,
'BR-tN': 1,
'BR-tq': 0,
'BR-OP': 1,
'BR-tP': 1,
'BR-QR': 1,
'BR-tr': 1}
Answered By - enke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.