I have a type(s1) = numpy.ndarray
. I want to create a dictionary by using the first column of s1
as key and rest as values to the key. The first column has repeated values.
Here is np.array.
s1 = np.array([[1L, 'R', 4],
[1L, 'D', 3],
[1L, 'I', 10],
[1L, 'K', 0.0],
[2L, 'R', 11],
[2L, 'D', 13],
[2L, 'I', 1],
[2L, 'K', 6],
[3L, 'R', 12],
[3L, 'D', 17],
[3L, 'I', 23],
[3L, 'K', 10]], dtype=object)
I want to get the following:
{'1':[['R',4],['D',3],['I',10],['K',0]],
'2':[['R',11],['D',13],['I',1],['K',6]],
'3':[['R',12],['D',17],['I',23],['K',10]]}
This is what I tried and got:
In [18]: {x[0]:[x[1],x[2]] for x in s1}
Out[18]: {1L: ['K', 0.0], 2L: ['D', 6], 3L: ['K', 10]}
I see the problem that the grouping column has repeated values. But I am unable to do the appending. What is the trick I am missing?
See Question&Answers more detail:
os