Python - 将数组列表转换为二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21046417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:53:53  来源:igfitidea点击:

Python - Conversion of list of arrays to 2D array

pythonarraysnumpy

提问by user3182080

I have a dataset that is formatted like this:

我有一个格式如下的数据集:

A=[(Num1,Num2,Num3), (Num4,Num5,Num6), (Num7,Num8,Num9)]

with

A.shape = (3,)

and I would like to convert this into a 2D numpy array:

我想将其转换为 2D numpy 数组:

A=[[Num1,Num2,Num3],[Num4,Num5,Num6],[Num7,Num8,Num9]]

with

A.shape = (3,3)

How do I do this, preferably without loops? Thanks.

我该怎么做,最好没有循环?谢谢。

回答by devalentino

I think like:

我认为:

A = map(lambda t: list(t), A)

回答by YS-L

Not sure if I understood the question correctly, but does this work for you?

不确定我是否正确理解了这个问题,但这对你有用吗?

import numpy as np
A = [[1,2,3],[4,5,6],[7,8,9]]
A = np.array(A)

If Ais a list of numpy array, how about this:

如果A是一个 numpy 数组列表,那么这个怎么样:

Ah = np.vstack(A)
Av = np.hstack(A)

回答by M4rtini

If what you have is a array of tupples. A (3,) array with dtype=object.
There is no way, that i am aware of, to elegantly unpack them into a (3,3) array through broadcasting. Converting back to a list, and then creating a new array seems the easiest.

如果您拥有的是一组元组。dtype=object 的 (3,) 数组。
据我所知,没有办法通过广播将它们优雅地解包到 (3,3) 数组中。转换回列表,然后创建一个新数组似乎是最简单的。

In [314]: data
Out[314]: array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=object)
In [315]: data.shape
Out[315]: (3L,)

data2 = np.empty((3,3), dtype=int)

#Neither of these work.
data2[:] = data[:]
data2[:] = data[:, None]

#This will work, but requires looping
data2[0,:] = data[0]
data2[1,:] = data[1]
data2[2,:] = data[2]

#This is the easies way i could find
data2 = np.array(data.tolist())

回答by Sergey Antopolskiy

If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d array. This can happen when your arrays are not of the same size. Example:

如果我正确理解您的要求,您会遇到 numpy 未将数组数组转换为二维数组的情况。当您的数组大小不同时,可能会发生这种情况。例子:

Automatic conversion to 2d array:

自动转换为二维数组:

import numpy as np
a = np.array([np.array([1,2,3]),np.array([2,3,4]),np.array([6,7,8])])
print a

Output:

输出:

>>>[[1 2 3]
    [2 3 4]
    [6 7 8]]

No automatic conversion (look for the change in the second subarray):

没有自动转换(寻找第二个子数组的变化):

import numpy as np
b = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
print b

Output:

输出:

>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]

I found a couple of ways of converting an array of arrays to 2d array. In any case you need to get rid of subarrays which have different size. So you will need a mask to select only "good" subarrays. Then you can use this mask with list comprehensions to recreate array, like this:

我找到了几种将数组数组转换为二维数组的方法。在任何情况下,您都需要摆脱具有不同大小的子数组。所以你需要一个掩码来只选择“好”的子数组。然后你可以使用这个带有列表推导式的掩码来重新创建数组,如下所示:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

c = np.array([element for (i,element) in enumerate(a) if mask[i]])

print a
print c

Output:

输出:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

Or you can delete "bad" subarrays and use vstack(), like this:

或者您可以删除“坏”子数组并使用 vstack(),如下所示:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

d = np.delete(a,np.where(mask==False))
e = np.vstack(d)

print a
print e

Output:

输出:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

I believe second method would be faster for large arrays, but I haven't tested the timing.

我相信第二种方法对于大型阵列会更快,但我还没有测试时间。