I have some nested list and/or tuple structure, e.g.:
x = [('foo',), ('bar', 'baz'), ('Ni!', 'Peng!', 'Nee-wom!')]
If I also have a dictionary whose keys include the lowest-level elements of that nested structure, e.g.:
d = {'foo': 99, 'bar': 50, 'baz': 100, 'Ni!': 1, 'Peng!': 2, 'Nee-wom!': 5}
Is there an easy way to produce a new object that retains the original nesting structure but has the values from the original replaced per the dict
? e.g.:
y = func(x, d)
y
# [(99,), (50, 100), (1, 2, 5)]
What is the func
needed here?
This is basically a find and replace operation based on the dictionary, but preserving the structure. I know how to flatten a nested structure but am not sure that's the right approach or how to reproduce it after. Maybe I'm way overthinking it...