Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inverting a dictionary of lists

Reply
Thread Tools

inverting a dictionary of lists

 
 
bpowah@gmail.com
Guest
Posts: n/a
 
      06-15-2007
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?

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-15-2007
En Fri, 15 Jun 2007 00:20:33 -0300, <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      06-15-2007
On Jun 14, 8:20 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
inverting custom cursor Niels Dybdahl Java 0 11-21-2005 09:07 AM
Inverting a Regular Expression in Validator spalding ASP .Net 1 01-28-2005 05:46 PM
Inverting a Regular Expression in Validator MWells ASP .Net 1 01-28-2005 02:44 AM
question on inverting text -> txet arctan Perl 6 10-16-2003 09:09 PM



Advertisments