[Python] Sort the values of a dictionary in order of connection

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to find a solution. Perhaps my words are bad chosen.

    I have a dictionary.

    • Each key has two values
    • All the keys have common values, except for two.
    • You'll understand, it's a chain. Now, I know I can't order a dictionary, so I'd like to know, if it's possible to arrange - in a list - the values.

    I've found something that comes close, but it still doesn't work.

    d= {
    'A': [tg, 17],
    'B': [59, 60],
    'C': [60, 61],
    'D': [57, tt],
    'E': [61, tg],
    'F': [tt, 59]
    }

    sorted_keys = sorted(d, key=lambda k: (d[k], k), reverse=True)


    The expected result is that: [17,tg,61,60,59,tt,57].

    [​IMG]

    Continue reading...

Compartilhe esta Página