I have a dictionary with thousands of elements of the following structure:
full_dict={'A': ['B','C','D','E'],
'B':['A','C','D','E','F','X']
'X':['W','Y','Z','S'],
'S':['W','K','T'],
...}
where every letter is a word. Every word can be both a key and a value (together with other words) for another dict element.
I am trying to find a "path" from one word to another. For example a path from 'A' to 'S' is A-B-X-S as S is among values of X, X in B and B in A.
Currently, I am using this code:
query=['A','S']
if 'S' in full_dict['A']:
print ('Found during 1st iteration')
else:
for i in full_dict['A']:
if 'S' in full_dict[i]:
print ('Found during 2nd iteration')
else:
for ii in full_dict[i]:
etc.
I do 10 iterations and it works fine but I wonder if there is a better way to do it. Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire