I'd like to take a dictionary of a dictionary containing floats, indexed by ints and convert it into a numpy.array for use with the numpy library. Currently I'm manually converting the values into two arrays, one for the original indexes and the other for the values. While I've looked at numpy.asarray
my conclusion has been that I must be doing something wrong with it. Could anyone show an example of how to properly convert such a creation? Don't have to use numpy.asarray
, anything will do.
from collections import defaultdict
foo = defaultdict( lambda: defaultdict(float) )
#Then "foo" is populated by several
#routines reading results from a DB
#
#As an example
foo[ 7104 ][ 3 ] = 4.5
foo[ 203 ][ 1 ] = 3.2
foo[ 2 ][ 1 ] = 2.7
I'd like to have just a multi-dimensional array of floats, not an array of dicts.
Edit:
Sorry for the delay. Here is the code that I was using to create the first array object containing just the values:
storedArray = numpy.asarray( reduce( lambda x,y: x + y, (item.values() for item in storedMapping.values() ) ) )
I was hoping someone might know a magic bullet that could convert a dict of dict into an array.