python - How to enumerate arbitrary nd-array with write enabled in Numpy? -


i trying fill arbitrary array increasing integers, failed:

import numpy np  sz = 3 sh = (30, 10, 10, 3)  xs = [] in range(sz):     xs.append(np.zeros(sh))  value = 0  in range(sz):     index, value in np.ndenumerate(xs[i]):         xs[i][list(index)] = value         value += 1 

after following code, arrays in xs list remains zero. why?

that's because counter , loop variable same: value.

for index, _ in np.ndenumerate(xs[i]):     xs[i][list(index)] = value     value += 1 

value takes on 0 every time loop iterates, resetting actual count value. hide underscore, don't believe need it.

alternatively, use np.arange , reshape:

xs = np.arange(sz * np.prod(sh)) 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -