Reputation: 11228
I have dictionary like this:
a = dict(zip( ['k1', 'k2', 'k3', 'k4'],
... [ [1,2,3,4], [10,20,30,40], [100,200,300,400], [1000,2000,3000,4000]])
>>> a
{'k1': [1, 2, 3, 4], 'k2': [10, 20, 30, 40], 'k3': [100, 200, 300, 400], 'k4': [1000, 2000, 3000, 4000]}
What I want to do: get values for several keys and create multidimensional numpy array from them. Something like this:
result = numpy.array( [a[x] for x in ('k1' , 'k3')]
I tried this code:
ar = numpy.array([])
for el in ['k1', 'k3']:
ar = numpy.r_[ar, num_dict[el]]
ar = ar.reshape(2,len(ar)/2)
But are there some built in functions or more elegant ways?
Upvotes: 1
Views: 119
Reputation: 11228
I need exactly one numpy array from data, so I can't find the way without loops. I create function:
def loadFromDict( fieldnames, dictionary ):
''' fieldnames - list of needed keys, dictionary - dict for extraction
result - numpy.array size of (number of keys, lengths of columns in dict)'''
ar = numpy.zeros( (len(fieldnames), len(dictionary[fieldnames[0]])) )
for c,v in enumerate(fieldnames,0):
ar[c,:] = dictionary[v]
return ar
In my case dictionary has the same length for all columns. Anyway it is easy to implement either they are different: use [len(v) for v in dictionary.values()]
to get all lengths, or find lengths for current keys.
Upvotes: 0
Reputation: 231395
A list of lists is normal input to np.array
, so your list comprehension makes sense.
In [382]: [a[x] for x in ['k1','k3']]
Out[382]: [[1, 2, 3, 4], [100, 200, 300, 400]]
Or for the whole dictionary
In [385]: np.array(list(a.values())) # list required in py3
Out[385]:
array([[1000, 2000, 3000, 4000],
[ 1, 2, 3, 4],
[ 10, 20, 30, 40],
[ 100, 200, 300, 400]])
Normally dictionary items are selected one by one as in the comprehension. operator
has a convenience class for fetching several keys with one call (I don't think it makes much difference in speed):
In [386]: import operator
In [387]: operator.itemgetter('k1','k3')(a)
Out[387]: ([1, 2, 3, 4], [100, 200, 300, 400])
I don't think the iteration with r_
is a good choice. r_
is just a cover for concatenate
. If you must iterate, repeated concatante
is slower. It's better to build a list, and make the array at the end (as in the list comprehension).
Upvotes: 1