【问题标题】:Reshape list/sequence of integers and arrays to array将整数和数组的列表/序列重塑为数组
【发布时间】:2017-09-07 19:34:25
【问题描述】:

我有多个格式列表 (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 这样的函数对序列也没有帮助......

最聪明的方法是什么?

非常感谢!

【问题讨论】:

  • 请注意:这个特定列表中的整数计数是 [0,2,3,4,5],在其他列表中它将是 [2,4,5] 或者不是。稍后在我的脚本中,我需要考虑这些因素。

标签: python arrays list numpy reshape


【解决方案1】:

您的列表理解会生成包含两个元素的列表,其中第二个元素是一个列表。例如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

【讨论】:

  • 好的,但是,在这种情况下,之前的“计数”现在将是 0、1、2、...、len(mylist)-1。在我后来的脚本中,我需要实际的计数。在本例中:0、2、3、4、5
  • 在你的帮助下,peak_corr_cell01_BR_sorted = np.ones((len(peak_corr_cell01_BR),len(corr_mean)+1)) for i in range(len(peak_corr_cell01_BR)): peak_corr_cell01_BR_sorted[i,1:] = peak_corr_cell01_BR[i][1] peak_corr_cell01_BR_sorted[i,0] = peak_corr_cell01_BR[i][0] 做到了!
【解决方案2】:

在威廉的帮助下:

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]

做到了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 2021-03-21
    • 2023-02-13
    • 1970-01-01
    • 2019-09-12
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多