Reputation: 141
I am facing a problem with one of my python script when trying to convert a list of lists to a dictionary of lists.
Here is what I have: a list of lists of ID numbers.
a = [ [0,1,2,5] , [4,5] , [0,4] , [2,5] ]
And I need to convert it in a dictionary of lists, in which keys correspond to each ID number and values are the list of ID numbers found with the key in the previous list of lists. That would be:
b = { 0 : [1,2,5,4] , 1 : [0,2,5] , 2 : [0,1,5] , 4 : [0,5] , 5 : [0,1,2,4] }
My original list of lists (a) is composed of 70,000 lists, and each of them consists on 2 to 5000 ID numbers (about 50,000 different ID numbers in total).
I came up with a very nasty code (tons of for loops) that requires about 3mn to perform this conversion. Would anyone have a suggestion to do it in a efficient (i.e. fast) way ? There might be a python library I don't know about.
thanks
Upvotes: 0
Views: 72
Reputation: 27283
There is a way of doing this in one "line":
b = {
key: list({
val
for relevant in (candidate for candidate in a if key in candidate)
for val in relevant
if key != val
}) for sublist in a for key in sublist
}
But a solution with nested loops might actually be faster.
The weird list({...})
construction is there to ensure the elements are listed only once per list (by collecting them in a set first).
Upvotes: 1
Reputation: 198324
I can't think of a way faster than this:
from collections import defaultdict
b = defaultdict(set)
for subl in a:
for el in subl:
for other_el in subl:
if other_el != el:
b[el].add(other_el)
You end up with sets as dict values, but if you need lists it is easy enough to do:
b = { k: list(v) for k, v in b.items() }
Upvotes: 1