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? 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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:
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |