1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python reverse dictionary key value reference relationships

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    If I have a following dictionary:

    {"1": {"2", "3"}, "2": {"3", "4"}, "3": {"2", "4"}}


    In this dictionary, the key references the values in the set.

    How do I construct a new dictionary which defines the references to the values above:

    {"1": {}, "2": {"1", "3"}, "3": {"1", "2"}, "4": {"2", "3"}}


    This is what I currently do:

    for k,v in data.items():
    if len(v) > 0:
    for p in v:
    references[p].add(k)
    else:
    for p,v in references.items():
    v.add(k)


    Any better / optimized /pythonic way of doing it?

    Continue reading...

Compartilhe esta Página