Unexpected behavour when making array of 2D arrays, of similar dimension

妖精的绣舞 提交于 2019-12-01 21:39:51

Simply printing out the arrays should fairly quickly allow you too see what happened.

As to the question of where did the last dimension go. Since the size of that dimension has variable length. Numpy wont create a new dimension for it, it will simply create an array of objects (where the object is a list) of varying length.

In the showArrayOfList(2,4,4) case your array looks like this:

First row:
[array([ 0., 0.]) array([ 0., 0.]) array([ 0., 0.]) array([ 0., 0.])]

second to fourth row:
[array([ 0., 0., 0., 0.]) array([ 0., 0., 0., 0.]) array([ 0., 0., 0., 0.]) array([ 0., 0., 0., 0.])]

A more consistent way of creating wArray is to initialize it to a (4,) object array, and fill it term by term:

n = len(wlist)
wArray = np.empty((n,), dtype='O')
for i in range(n):
    wArray[i] = wlist[i]

This isn't as pretty as asarray(wlist), but it splits the 3 dimensions in the same 1,2 manner regardless of what a,b,c are.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!