将 list/sequence 个整数和数组重塑为数组
Reshape list/sequence of integers and arrays to array
我有多个格式列表
(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 等函数对序列也没有帮助...
完成此任务最明智的方法是什么?
非常感谢!
您的列表理解生成包含两个元素的列表,其中第二个元素是一个列表。例如对于 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]]
不过,您可以通过以下方式简化事情:
<b>mylist_sorted[:,0] = np.arange(len(mylist))</b>
for i in range(len(mylist)):
mylist_sorted[i<b>,1:]</b> = <b>mylist[i][1]</b>
根据您的样本输入,这将产生:
>>> 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]])
编辑:如果 "counts" 不是 0、1、2,...您可以更改上面的代码片段:
for <b>i, (left, right)</b> in <b>enumerate(mylist)</b>:
<b>mylist_sorted[i,0] = left</b>
mylist_sorted[i,1:] = <b>right</b>
在威廉的帮助下:
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]
做到了!
我有多个格式列表 (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 等函数对序列也没有帮助...
完成此任务最明智的方法是什么?
非常感谢!
您的列表理解生成包含两个元素的列表,其中第二个元素是一个列表。例如对于 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]]
不过,您可以通过以下方式简化事情:
<b>mylist_sorted[:,0] = np.arange(len(mylist))</b>
for i in range(len(mylist)):
mylist_sorted[i<b>,1:]</b> = <b>mylist[i][1]</b>
根据您的样本输入,这将产生:
>>> 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]])
编辑:如果 "counts" 不是 0、1、2,...您可以更改上面的代码片段:
for <b>i, (left, right)</b> in <b>enumerate(mylist)</b>:
<b>mylist_sorted[i,0] = left</b>
mylist_sorted[i,1:] = <b>right</b>
在威廉的帮助下:
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]
做到了!