inverting a dictionary of lists

Discussion in 'Python' started by bpowah@gmail.com, Jun 15, 2007.

  1. Guest

    I searched for an hour and don't see a solution to this (i assume
    somewhat common) problem.

    I have a very large dictionary of lists:
    d = {a:[1,2], b:[2,3], c:[3]}
    and i want to reverse the associativity of the integers thusly:
    inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]}

    my solution expands the original dict into two lists of keys and list
    elements:
    list1: [a,a,b,b,c]
    list2: [1,2,2,3,3]
    then recombines them with the reverse operation.

    but this takes too much time and a lot of memory.
    I wonder if anyone can point me to a more efficient solution?
    , Jun 15, 2007
    #1
    1. Advertising

  2. En Fri, 15 Jun 2007 00:20:33 -0300, <> escribió:

    > I searched for an hour and don't see a solution to this (i assume
    > somewhat common) problem.
    >
    > I have a very large dictionary of lists:
    > d = {a:[1,2], b:[2,3], c:[3]}
    > and i want to reverse the associativity of the integers thusly:
    > inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]}
    >
    > my solution expands the original dict into two lists of keys and list
    > elements:
    > list1: [a,a,b,b,c]
    > list2: [1,2,2,3,3]
    > then recombines them with the reverse operation.
    >
    > but this takes too much time and a lot of memory.
    > I wonder if anyone can point me to a more efficient solution?


    py> d = dict(a=[1,2], b=[2,3], c=[3])
    py> result = {}
    py> for k,v in d.iteritems():
    .... for item in v:
    .... result.setdefault(item, []).append(k)
    ....
    py> result
    {1: ['a'], 2: ['a', 'b'], 3: ['c', 'b']}
    py>

    You may use collections.defaultdict too - search some recent posts.

    --
    Gabriel Genellina
    Gabriel Genellina, Jun 15, 2007
    #2
    1. Advertising

  3. On Jun 14, 8:20 pm, wrote:
    > I have a very large dictionary of lists:
    > d = {a:[1,2], b:[2,3], c:[3]}
    > and i want to reverse the associativity of the integers thusly:
    > inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]}




    Try using setdefault:

    >>> d = {'a':[1,2], 'b':[2,3], 'c':[3]}
    >>> r = {}
    >>> for k in d:

    for e in d[k]:
    r.setdefault(e, []).append(k)

    >>> r

    {1: ['a'], 2: ['a', 'b'], 3: ['c', 'b']}


    Raymond
    Raymond Hettinger, Jun 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arctan
    Replies:
    6
    Views:
    1,026
    arctan
    Oct 16, 2003
  2. MWells
    Replies:
    1
    Views:
    641
    spalding
    Jan 28, 2005
  3. spalding
    Replies:
    1
    Views:
    408
    Peter Blum
    Jan 28, 2005
  4. Niels Dybdahl

    inverting custom cursor

    Niels Dybdahl, Nov 21, 2005, in forum: Java
    Replies:
    0
    Views:
    397
    Niels Dybdahl
    Nov 21, 2005
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    399
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page