代码之家  ›  专栏  ›  技术社区  ›  knut_h

将整数和数组的列表/序列重塑为数组

  •  2
  • knut_h  · 技术社区  · 7 年前

    我有多个格式列表 我想把它们转换成数组,这样它们就可以读取

    如果这有助于理解我的意思,下面是一个示例列表:

    mylist = [[0, array([ 1.        ,  0.73475787,  0.36224658,  0.08579446, -0.11767365,
           -0.09927562,  0.17444341,  0.47212111,  1.00584593,  1.69147789,
            1.89421069,  1.4718292 ])], [2, array([ 1.        ,  0.68744907,  0.38420843,  0.25922927,  0.04719614,
            0.00841919,  0.21967246,  0.22183329,  0.28910002,  0.54637077,
           -0.04389335, -1.33445338])], [3, array([ 1.        ,  0.77854922,  0.41093192,  0.0713814 , -0.08194854,
           -0.07885753,  0.1491798 ,  0.56297583,  1.0759857 ,  1.57149366,
            1.37958867,  0.64409152])], [4, array([ 1.        ,  0.35988801,  0.18939934,  0.45618952,  0.24415997,
           -0.33527807, -0.35296085, -0.41893959, -0.48589674, -0.66222111,
           -0.58601528, -1.14922484])], [5, array([ 1.        ,  0.09182989,  0.14988215, -0.1272845 ,  0.12154707,
           -0.01194815, -0.06136953,  0.18783772,  0.46631855,  0.78850281,
            0.64755372,  0.69757144])]]
    

    我试过(为了其中一个列表)

    mylist_sorted = np.ones((len(mylist),len(arrays)+1))
    for i in range(len(mylist)):
        mylist_sorted[i] = [i,[mylist[i][1][j] for j in range(len(arrays))]]
    

    ValueError: cannot copy sequence with size 2 to array axis with dimension n+1
    

    实现这一点最聪明的方法是什么?

    2 回复  |  直到 7 年前
        1
  •  0
  •   willeM_ Van Onsem    7 年前

    你的 列表理解 包含两个元素的列表 其中第二个是一个列表。例如 i = 1

    >>> [i,[mylist[i][1][j] for j in range(12)]]
    [1, [1.0, 0.68744907, 0.38420842999999999, 0.25922927000000001, 0.047196139999999998, 0.00841919, 0.21967246000000001, 0.22183328999999999, 0.28910002000000001, 0.54637077000000001, -0.043893349999999998, -1.33445338]]
    

    然而,你可以通过写作使事情变得更容易:

    mylist_sorted[:,0] = np.arange(len(mylist))
    for i in range(len(mylist)):
        mylist_sorted[i,1:] = mylist[i][1]

    通过示例输入,这将产生:

    >>> mylist_sorted
    array([[ 0.        ,  1.        ,  0.73475787,  0.36224658,  0.08579446,
            -0.11767365, -0.09927562,  0.17444341,  0.47212111,  1.00584593,
             1.69147789,  1.89421069,  1.4718292 ],
           [ 1.        ,  1.        ,  0.68744907,  0.38420843,  0.25922927,
             0.04719614,  0.00841919,  0.21967246,  0.22183329,  0.28910002,
             0.54637077, -0.04389335, -1.33445338],
           [ 2.        ,  1.        ,  0.77854922,  0.41093192,  0.0713814 ,
            -0.08194854, -0.07885753,  0.1491798 ,  0.56297583,  1.0759857 ,
             1.57149366,  1.37958867,  0.64409152],
           [ 3.        ,  1.        ,  0.35988801,  0.18939934,  0.45618952,
             0.24415997, -0.33527807, -0.35296085, -0.41893959, -0.48589674,
            -0.66222111, -0.58601528, -1.14922484],
           [ 4.        ,  1.        ,  0.09182989,  0.14988215, -0.1272845 ,
             0.12154707, -0.01194815, -0.06136953,  0.18783772,  0.46631855,
             0.78850281,  0.64755372,  0.69757144]])
    

    编辑 :如果“计数”不是0、1、2。。。您可以更改上述代码片段:

    for i, (left, right) in enumerate(mylist):
        mylist_sorted[i,0] = left
        mylist_sorted[i,1:] = right
        2
  •  0
  •   knut_h    7 年前

    在威廉的帮助下:

    mylist_sorted = np.ones((len(mylist),len(arrays)+1))
    for i in range(len(mylist)):
        mylist_sorted[i,1:] = mylist[i][1]
        mylist_sorted[i,0] = mylist[i][0]
    

    成功了!