简体   繁体   English

无法使用numpy展平数组

[英]Can not flatten the array with numpy

I have an output which is as follows : 我的输出如下:

data = [array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
        1.65028784, 0.24901275]),
 array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
        0.00822115, 1.96379539]),
 array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
        1.43616476])]

That is a list of 3 arrays. 这是3个数组的列表。 I have to flatten the array. 我必须展平阵列。 This should be simple as data.flatten("F") But it does not work 这应该像data.flatten("F")一样简单,但是它不起作用

The output should contain the first element of each array then the second and so on. 输出应包含每个数组的第一个元素,然后是第二个,依此类推。

something like: 1,1.83665564,1.14112001,0.09442164,1.85090352 and so on. 像: 1,1.83665564,1.14112001,0.09442164,1.85090352等。 How can i do that 我怎样才能做到这一点

Update : 更新:

the output I am getting with given code as follows 我得到的输出与给定的代码如下

array([array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
       1.65028784, 0.24901275]),
       array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
       0.00822115, 1.96379539]),
       array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
       1.43616476])], dtype=object)
from numpy import array
data = [array([1.        , 1.14112001, 0.7205845 , 1.41211849, 0.46342708,
        1.65028784, 0.24901275]),
 array([1.83665564, 0.09442164, 1.95637593, 0.01196838, 1.99991186,
        0.00822115, 1.96379539]),
 array([0.08347845, 1.85090352, 0.23174534, 1.67022918, 0.44121095,
        1.43616476])]
max=len(max(data,key=len))
final_list=[]
for index in range (0,max):
        final_list.extend([a[index] for a in data if len(a)>index])
print(final_list)

Output: 输出:

[1.0, 1.83665564, 0.08347845, 1.14112001, 0.09442164, 1.85090352, 0.7205845, 1.95637593, 0.23174534, 1.41211849, 0.01196838, 1.67022918, 0.46342708, 1.99991186, 0.44121095, 1.65028784, 0.00822115, 1.43616476, 0.24901275, 1.96379539]

Don't know if best solution, but you can use np.resize to reshape all arrays to the same length and stack them vertically. 不知道最好的解决方案,但是您可以使用np.resize将所有数组np.resize为相同的长度,并垂直堆叠它们。 Then use .T to flatten them column based, and use a mask to exclude the values created by np.resize : 然后使用.T使它们变平,并使用遮罩排除np.resize创建的值:

l = max(x.size for x in data)
masks = [np.arange(l) < x.size for x in data]
np.vstack(np.resize(x, l) for x in data).T.flatten()[np.vstack(masks).T.flatten()]

>> array([1.        , 1.83665564, 0.08347845, 1.14112001, 0.09442164,
   1.85090352, 0.7205845 , 1.95637593, 0.23174534, 1.41211849,
   0.01196838, 1.67022918, 0.46342708, 1.99991186, 0.44121095,
   1.65028784, 0.00822115, 1.43616476, 0.24901275, 1.96379539])

Use itertools.zip_longest to get elements in different-length arrays in order (see this post ) and then use list comprehension to flatten the resulting tuples. 使用itertools.zip_longest依次获取不同长度数组中的元素(请参阅此文章 ),然后使用列表推导来展平结果元组。

In Python 2 we should use itertools.izip_longest instead. 在Python 2中,我们应该改用itertools.izip_longest。

>>> from itertools import zip_longest
>>> from numpy import array

# three sample arrays make up a list from 1 to 11
>>> data = array([array([1,4,7,10]), array([2,5,8,11]), array([3,6,9])])
>>> temp = list(zip_longest(*data, fillvalue=None))
>>> temp
[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, None)]

>>> [i for tup in temp for i in tup if i is not None]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM