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

后端 未结 2 1477
日久生厌
日久生厌 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:40

    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.

提交回复
热议问题