python - Concetenating arrays in a dictionary -
say have python dictionary 100's of keys. each key, dictionary holds 2d array.
all these 2d arrays have same number of rows. how can concatenate these arrays efficiently in final 2d array along column axis?
is worth going through pandas this? if so, how?
e.g.
from collections import ordereddict() dct = ordereddict() key in xrange(3): dct[key] = np.random.randint(3,size=(2,np.random.randint(10))) # print dictionary: > dict(dct) {0: array([[1, 0, 2, 2, 2, 1, 0], [1, 2, 2, 1, 1, 1, 0]]), 1: array([[2, 1, 0, 1, 1], [1, 1, 2, 2, 2]]), 2: array([[2], [0]])}
the result of concatenation should be:
array([[1, 0, 2, 2, 2, 1, 0, 2, 1, 0, 1, 1, 2], [1, 2, 2, 1, 1, 1, 0, 1, 1, 2, 2, 2, 0]])
the hstack
function want.
since have unordered dict, implied order in keys, want this:
>>> dct defaultdict(<built-in function array>, {0: array([[0, 1, 2, 0, 2, 2, 0], [0, 0, 0, 2, 0, 0, 2]]), 1: array([[0, 1, 2, 0, 0], [0, 0, 1, 2, 2]]), 2: array([[1, 1, 0, 0], [0, 1, 1, 2]])}) >>> np.hstack(dct[k] k in sorted(dct)) array([[0, 1, 2, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1, 0, 0], [0, 0, 0, 2, 0, 0, 2, 0, 0, 1, 2, 2, 0, 1, 1, 2]])
now you've changed question use ordereddict
instead of defaultdict
, have values in right order, can of course use dct.values()
instead.
Comments
Post a Comment