Is there any method available in python to obtain the key that a value belongs to within a nested dictionary?
An example would be:
dic = {1 :
{2 :
{3 : 4}},
5 :
{6 :
{7 : 8}}
}
Where I would then want to know the path one needs to take to reach either 4 or 8.
This would look something like:
find_path(dic, 8)
which should return something like
5, 6, 7 # since dic[5][6][7] leads to 8.
For context: I am trying to create 60^5 game states for an AI that I intend to implement for a game. I need to analyze all game states at a depth of 5 to determine which is best. Then, in order to reach the state at depth 5, I need to know what steps to take at depth 1, 2, 3 and 4 in order to reach this game state. I don't know whether dictionaries are optimal to achieve this, so would love to hear some other suggestions if possible.
find_path()? Can you share it?