How do I convert a simple list of lists into a numpy array? The rows are individual sublists and each row contains the elements in the sublist.
如何将简单的列表列表转换为numpy数组?行是单个子列表,每行包含子列表中的元素。
5 个解决方案
#1
94
If your list of lists contains lists with varying number of elements then the answer of Ignacio Vazquez-Abrams will not work. Instead there are 3 options:
如果您的列表列表包含具有不同数量元素的列表,则Ignacio Vazquez-Abrams的答案将不起作用。相反,有3个选项:
1) Make an array of arrays:
1)创建一个数组数组:
x=[[1,2],[1,2,3],[1]]
y=numpy.array([numpy.array(xi) for xi in x])
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'numpy.ndarray'>
2) Make an array of lists:
2)制作一系列清单:
x=[[1,2],[1,2,3],[1]]
y=numpy.array(x)
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'list'>
3) First make the lists equal in length:
3)首先使列表的长度相等:
x=[[1,2],[1,2,3],[1]]
length = len(sorted(x,key=len, reverse=True)[0])
y=numpy.array([xi+[None]*(length-len(xi)) for xi in x])
y
>>>array([[1, 2, None],
>>> [1, 2, 3],
>>> [1, None, None]], dtype=object)
#2
75
>>> numpy.array([[1, 2], [3, 4]])
array([[1, 2], [3, 4]])
#3
17
As this is the top search on Google for converting a list of lists into a Numpy array, I'll offer the following despite the question being 4 years old:
由于这是谷歌将列表列表转换为Numpy阵列的最佳搜索,我将提供以下内容,尽管问题是4年前:
>>> x = [[1, 2], [1, 2, 3], [1]]
>>> y = numpy.hstack(x)
>>> print(y)
[1 2 1 2 3 1]
When I first thought of doing it this way, I was quite pleased with myself because it's soooo simple. However, after timing it with a larger list of lists, it is actually faster to do this:
当我第一次想到这样做时,我对自己很满意,因为它太简单了。但是,在使用更大的列表列表对其进行计时后,实际上执行此操作会更快:
>>> y = numpy.concatenate([numpy.array(i) for i in x])
>>> print(y)
[1 2 1 2 3 1]
Note that @Bastiaan's answer #1 doesn't make a single continuous list, hence I added the concatenate
.
请注意,@ Bastiaan的答案#1没有一个连续的列表,因此我添加了连接。
Anyway...I prefer the hstack
approach for it's elegant use of Numpy.
无论如何......我更喜欢hstack方法,因为它优雅地使用了Numpy。
#4
14
It's as simple as:
它很简单:
>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
[3, 4]])
#5
2
Again, after searching for the problem of converting nested lists with N levels into an N-dimensional array I found nothing, so here's my way around it:
再次,在搜索了将N级别的嵌套列表转换为N维数组的问题后,我什么也没找到,所以这是我的方法:
import numpy as np
new_array=np.array([[[coord for coord in xk] for xk in xj] for xj in xi], ndmin=3) #this case for N=3
#1
94
If your list of lists contains lists with varying number of elements then the answer of Ignacio Vazquez-Abrams will not work. Instead there are 3 options:
如果您的列表列表包含具有不同数量元素的列表,则Ignacio Vazquez-Abrams的答案将不起作用。相反,有3个选项:
1) Make an array of arrays:
1)创建一个数组数组:
x=[[1,2],[1,2,3],[1]]
y=numpy.array([numpy.array(xi) for xi in x])
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'numpy.ndarray'>
2) Make an array of lists:
2)制作一系列清单:
x=[[1,2],[1,2,3],[1]]
y=numpy.array(x)
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'list'>
3) First make the lists equal in length:
3)首先使列表的长度相等:
x=[[1,2],[1,2,3],[1]]
length = len(sorted(x,key=len, reverse=True)[0])
y=numpy.array([xi+[None]*(length-len(xi)) for xi in x])
y
>>>array([[1, 2, None],
>>> [1, 2, 3],
>>> [1, None, None]], dtype=object)
#2
75
>>> numpy.array([[1, 2], [3, 4]])
array([[1, 2], [3, 4]])
#3
17
As this is the top search on Google for converting a list of lists into a Numpy array, I'll offer the following despite the question being 4 years old:
由于这是谷歌将列表列表转换为Numpy阵列的最佳搜索,我将提供以下内容,尽管问题是4年前:
>>> x = [[1, 2], [1, 2, 3], [1]]
>>> y = numpy.hstack(x)
>>> print(y)
[1 2 1 2 3 1]
When I first thought of doing it this way, I was quite pleased with myself because it's soooo simple. However, after timing it with a larger list of lists, it is actually faster to do this:
当我第一次想到这样做时,我对自己很满意,因为它太简单了。但是,在使用更大的列表列表对其进行计时后,实际上执行此操作会更快:
>>> y = numpy.concatenate([numpy.array(i) for i in x])
>>> print(y)
[1 2 1 2 3 1]
Note that @Bastiaan's answer #1 doesn't make a single continuous list, hence I added the concatenate
.
请注意,@ Bastiaan的答案#1没有一个连续的列表,因此我添加了连接。
Anyway...I prefer the hstack
approach for it's elegant use of Numpy.
无论如何......我更喜欢hstack方法,因为它优雅地使用了Numpy。
#4
14
It's as simple as:
它很简单:
>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
[3, 4]])
#5
2
Again, after searching for the problem of converting nested lists with N levels into an N-dimensional array I found nothing, so here's my way around it:
再次,在搜索了将N级别的嵌套列表转换为N维数组的问题后,我什么也没找到,所以这是我的方法:
import numpy as np
new_array=np.array([[[coord for coord in xk] for xk in xj] for xj in xi], ndmin=3) #this case for N=3