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

后端 未结 2 1478
日久生厌
日久生厌 2021-01-21 09:10

MWE:

def showArrayOfList(a,b,c):
    wlist = [np.zeros((szNext,szThis)) for (szThis,szNext) in [(a,b),(b,b),(b,b),(b,c)]]

    print \"wlist:\",         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-21 09:27

    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.])]

提交回复
热议问题