Wednesday, 21 August 2013

convert n equal length lists to dictionary with unique keys from one list

convert n equal length lists to dictionary with unique keys from one list

I have n lists of equal length representing values of database rows. The
data is pretty complicated, so i'll present simplified values in the
example.
Essentially, I want to map the values of these lists (a,b,c) to a
dictionary where the keys are the set of the list (id).
Example lists:
id = [1,1,1,2,2,2,3,3,3]
a = [1,2,3,4,5,6,7,8,9]
b = [10,11,12,13,14,15,16,17,18]
c = [20,21,22,23,24,25,26,27,28]
Needed dictionary output:
{id:[[a],[b],[c]],...}
{'1':[[1,2,3],[10,11,12],[20,21,22]],'2':[[4,5,6],[13,14,15],[23,24,25]],'3':[[7,8,9],[16,17,18],[26,27,28]]}
The dictionary now has a list of lists for the values in the original
a,b,c subsetted by the unique values in the id list which is now the
dictionary key.
I hope this is clear enough.

No comments:

Post a Comment