fork download
  1. d = {
  2. 'B': [59, 60],
  3. 'A': ['tg', 17],
  4. 'C': [60, 61],
  5. 'D': [57, 'tt'],
  6. 'E': [61, 'tg'],
  7. 'F': ['tt', 59],
  8. }
  9.  
  10. # we build a inverted dictonnary to return the list
  11. # the tuple of values [v1, v2] for each value v{1,2}
  12. # { 17: [['tg', 17]],
  13. # 57: [[57, 'tt']],
  14. # 'tg': [['tg', 17], [61, 'tg']],
  15. # 60: [[59, 60], [60, 61]], ... }
  16. dico = dict()
  17. for i in d.values():
  18. for j in i:
  19. dico[j] = dico.get(j, []) + [i]
  20.  
  21. print(dico)
  22.  
  23. # we get a piece of the list, that easy there is one instance
  24. m = min(l := [j for i in d.values() for j in i], key=l.count) # py 3.8
  25. print(m, l, l.count(57))
  26.  
  27. z = [m]
  28. v1, v2 = dico[m][0]
  29. v = v1 if v2 == m else v2 # we get the other value of the tuple
  30. z.append(v)
  31.  
  32. while len(dico[v]) > 1: # until we a not a the end of the chain
  33. v1, v2 = [i for i in dico[v] if m not in i][0]
  34. m = v
  35. v = v1 if v2 == m else v2
  36. z.append(v)
  37.  
  38. print(z)
Success #stdin #stdout 0.09s 14112KB
stdin
Standard input is empty
stdout
{59: [[59, 60], ['tt', 59]], 60: [[59, 60], [60, 61]], 'tg': [['tg', 17], [61, 'tg']], 17: [['tg', 17]], 61: [[60, 61], [61, 'tg']], 57: [[57, 'tt']], 'tt': [[57, 'tt'], ['tt', 59]]}
17 [59, 60, 'tg', 17, 60, 61, 57, 'tt', 61, 'tg', 'tt', 59] 1
[17, 'tg', 61, 60, 59, 'tt', 57]