473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

inverting a dictionary of lists

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 '07 #1
2 1378
En Fri, 15 Jun 2007 00:20:33 -0300, <bp****@gmail.comescribió:
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?
pyd = dict(a=[1,2], b=[2,3], c=[3])
pyresult = {}
pyfor k,v in d.iteritems():
.... for item in v:
.... result.setdefault(item, []).append(k)
....
pyresult
{1: ['a'], 2: ['a', 'b'], 3: ['c', 'b']}
py>

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

--
Gabriel Genellina

Jun 15 '07 #2
On Jun 14, 8:20 pm, bpo...@gmail.com 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

Jun 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
15
by: Andy C | last post by:
I am new to python, so please bear with me if I am making some conceptual error. Basically I want to create a graph with an adjacency list representation, but I don't want any of the adjacency...
2
by: John Mudd | last post by:
I must be missing something here. It's clearly faster to lookup an item directly in a dictionary than to scan through a list. So when I have a large lookup table I always load it in the form of a...
15
by: Irmen de Jong | last post by:
Hi I have this dict that maps a name to a sequence of other names. I want to have it reversed, i.e., map the other names each to the key they belong to (yes, the other names are unique and they...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
1
by: Eran | last post by:
Hi, I have a huge data structure, which I previosly stored in a Dictionary<int, MyObj> MyObj is relatively small (2 int, 1 DateTime, 1 bool). The dictionary I am using is quite large...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.