将整数列表/序列和数组重新整形为数组

时间:2017-09-07 19:34:26

标签: python arrays list numpy reshape

我有多个格式列表 (count_i,1dim-array_i) 我想将它们转换为数组,以便它们读取

[count_i,1dim-array_i [0],1dim-array_i [1],1dim-array_i [2],...,1dim-array_i [n]]

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

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

像numpy.reshape这样的函数对序列没有帮助......

实现这一目标的最明智的方法是什么?

非常感谢!

2 个答案:

答案 0 :(得分:0)

您的列表理解生成带有两个元素的列表,其中第二个列表是列表。例如,对于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

答案 1 :(得分:0)

在Willem的帮助下:

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]

做到了!