Say I have a nested dictionary like so:
{
'a': {
'z': [
{
'x': 1,
'y': [
[{'a': 10}, {'f': 21}],
{'m': 100},
]
}
]
}
'm': {
'x': 100
}
}
And let's say I have a list of paths that have no relative order to each other:
[('a', 'z', 'x'), ('m', 'x'), ('a', 'z', 'y', 'a'), ...]
And I want to update whatever value the final key is for all these paths to some value val. Notice that in the third tuple, the last key is a, but it sits in a list.
In my data, these assumptions can be made:
- key names can be repeated in a single path (notice
'a'is the source and destination node in the third tuple) - destination "nodes" are always a string, int, float, boolean, i.e. - never ending on a list or another dictionary
- lists only hold other lists or dictionaries
My thoughts:
- My current implementation is a brute force approach which is too slow for my requirements: iterate from source to destination for every single one of these paths, detecting if I am on a list or dictionary.
- Ideally, I would back track and visit nearby nodes that need to be visited as well, so that I don't have to start from the top level again. This kind of reminds me of visiting multiple stops/cities on a trip.
- I was also thinking of a trie, but I think that would still mean starting from source to destination. Also I don't think a trie would work since we have lists.
Any thoughts?