After parsing a .txt file of strings, I have created two dictionaries.
Dict1 is a pairing of key(string) : value(list[string]].
Dict2 is an enumeration of dict1 such that key(int) : value(string--the key from dict1)
I am trying to create a third dictionary with the values from dict1 as a vector/list in the form key(int): Value (list[int])
dict1 = {'Foo': ['Lorem', 'Ipsum'], 'Bar':['Dolor', 'Sit', 'Amet'], 'Baz':['Consectetur', 'Adipiscing', 'Elit', 'Sed'], 'Lorem':['Foo', 'Do']}
dict2 = {1: 'Foo', 2: 'Bar', 3: 'Baz', 4: 'Lorem', 5: 'Ipsum', 6: 'Dolor', 7: 'Sit', 8: 'Amet', 9: 'Consectetur', 10: 'Adipiscing', 11: 'Elit', 12: 'Sed', 13: 'Do'}
For instance, I want
dict3 = {1: [4, 5], 2: [6, 7, 8], 3:[9, 10, 11, 12], 4:[1, 13]... & so on}
What would be an efficient way to go about this?
CodePudding user response:
The instinct you have in your comment is correct, start by making a new mapping that is the reverse of dict2:
dict4 = {v: k for k, v in dict2.items()}
Then you can create dict3 with the following (admittedly complex) nesting of dict
and list
comprehensions:
dict3 = {k: [dict4[v2] for v2 in dict1[v] if v2 in dict4] for k, v in dict2.items() if v in dict1}
CodePudding user response:
# original dicts
dict1 = {'Foo': ['Lorem', 'Ipsum'], 'Bar':['Dolor', 'Sit', 'Amet']}
dict2 = {1: 'Foo', 2: 'Bar', 3: 'Baz', 4: 'Lorem', 5: 'Ipsum', 6: 'Dolor', 7: 'Sit', 8: 'Amet' }
# invert keys and vals from dict2
d2_index = { v: i for i, v in zip( dict2.keys(), dict2.values() ) }
# map indices to values
{
d2_index[ k ]: [
d2_index[ v ]
for v in vs
]
for k, vs in dict1.items()
}
CodePudding user response:
There's not going to be a particularly efficient way to approach this since this is almost by design a O(n) or O(n * m) problem. The best way is going to be to iterate through dict1
to access each key's int
value, and do the same for each value in the associated value list:
dict3 = dict()
for key, value in dict1.items():
key_int = dict2[key]
value_ints = [dict2[i] for i in value]
dict3[key_int] = value_ints
or as a comprehension:
dict3 = { dict2[key]: [dict2[i] for i in value] for key, value in dict1.items() }
Make sure you are wrapping either solution in a try
block to catch an KeyError
s that might be raised for a value in dict
that isn't present in dict2
CodePudding user response:
You can swap keys and values of dict2
dict1 = {'Foo': ['Lorem', 'Ipsum'], 'Bar':['Dolor', 'Sit', 'Amet']}
dict2 = {1: 'Foo', 2: 'Bar', 3: 'Lorem', 4: 'Ipsum', 5: 'Dolor', 6: 'Sit', 7: 'Amet'}
dict2_reversed = {value: key for key, value in dict2.items()}
Then use a dictionary comprehension to create the final dictionary. You also need a nested list comprehension for the values:
>>> {dict2_reversed[key]: [dict2_reversed[v] for v in value]
for key, value in dict1.items()}
{1: [3, 4], 2: [5, 6, 7]}