Reputation: 65
If I have one dictionary:
{'10103 - Baldwin, C': 'SFEN', '10115 - Wyatt, X': 'SFEN', '10172 - Forbes, I': 'SFEN'}
And another dictionary:
{'10103': ['CS 501', 'SSW 564', 'SSW 567', 'SSW 687'], '10115': ['CS 545', 'SSW 564', 'SSW 567', 'SSW 687'], '10172': ['SSW 555', 'SSW 567']}
What would be the best way to transfer the key from the first dictionary to the second dictionary so I get something like:
{'10103 - Baldwin, C': ['CS 501', 'SSW 564', 'SSW 567', 'SSW 687'], '10115 - Wyatt, X': ['CS 545', 'SSW 564', 'SSW 567', 'SSW 687'], '10172 - Forbes, I': ['SSW 555', 'SSW 567']}
I found this solution on another thread, but it requires a lookup of a value in the first dictionary to match the key in the second dictionary. If I just want to do a one-to-one transfer if I know that the order of the items in the dictionaries are the same, how could I do so?
lookup = {v: k for k, v in df2_dict.items()}
result = {lookup[k]: v for k, v in sorted_classes.items()}
Upvotes: 0
Views: 34
Reputation: 191758
Given dictionaries d1 and d2, you would need to parse out the number from the keys of d1 so that you can look it up in d2. From there, you can replace the values for each key
for k in d1.keys():
n, _ = k.split('-')
n = n.strip()
d1[k] = d2[n]
print(d1)
Upvotes: 2