Creating dictionary from numpy array
I'd do something similar in spirit to your dict(zip(keys, s1))
, with two minor changes.
First, we can use enumerate
, and second, we can call the sum
method of ndarray
s. Example:
>>> arr = np.arange(9).reshape(3,3)>>> arrarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])>>> arr.sum(axis=1)array([ 3, 12, 21])>>> dict(enumerate(arr.sum(axis=1))){0: 3, 1: 12, 2: 21}