问题:列表列表成numpy数组

如何将列表的简单列表转换为numpy数组?这些行是单独的子列表,每行包含该子列表中的元素。

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.


回答 0

如果列表列表包含元素数量不同的列表,则Ignacio Vazquez-Abrams的答案将不起作用。相反,至少有3个选项:

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)制作一个列表数组:

x=[[1,2],[1,2,3],[1]]
y=numpy.array(x)
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'list'>

3)首先使列表的长度相等:

x=[[1,2],[1,2,3],[1]]
length = max(map(len, x))
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)

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 at least 3 options:

1) Make an array of arrays:

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:

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:

x=[[1,2],[1,2,3],[1]]
length = max(map(len, x))
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)

回答 1

>>> numpy.array([[1, 2], [3, 4]]) 
array([[1, 2], [3, 4]])
>>> numpy.array([[1, 2], [3, 4]]) 
array([[1, 2], [3, 4]])

回答 2

由于这是Google上将列表列表转换为Numpy数组的热门搜索,尽管问题已经有4年之久,但我还是提供以下内容:

>>> x = [[1, 2], [1, 2, 3], [1]]
>>> y = numpy.hstack(x)
>>> print(y)
[1 2 1 2 3 1]

当我第一次想到以这种方式进行操作时,我对自己感到很满意,因为它非常简单。但是,在使用较大的列表列表进行计时之后,实际上这样做会更快:

>>> y = numpy.concatenate([numpy.array(i) for i in x])
>>> print(y)
[1 2 1 2 3 1]

请注意,@ Bastiaan的答案#1没有列出一个连续的列表,因此我添加了 concatenate

无论如何…我更喜欢这种hstack方法,因为它优雅地使用了Numpy。

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:

>>> 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.

Anyway…I prefer the hstack approach for it’s elegant use of Numpy.


回答 3

就像这样简单:

>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
       [3, 4]])

It’s as simple as:

>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
       [3, 4]])

回答 4

再一次,在搜索将具有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

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:

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

回答 5

我有一个长度相等的清单。即使Ignacio Vazquez-Abrams那样,对我来说答案也没有解决。我有一个一维numpy数组,其元素是列表。如果遇到相同的问题,可以使用以下方法

numpy.vstack

import numpy as np

np_array = np.empty((0,4), dtype='float')
for i in range(10)
     row_data = ...   # get row_data as list
     np_array = np.vstack((np_array, np.array(row_data)))

I had a list of lists of equal length. Even then Ignacio Vazquez-Abrams‘s answer didn’t work out for me. I got a 1-D numpy array whose elements are lists. If you faced the same problem, you can use the below method

Use numpy.vstack

import numpy as np

np_array = np.empty((0,4), dtype='float')
for i in range(10)
     row_data = ...   # get row_data as list
     np_array = np.vstack((np_array, np.array(row_data)))

回答 6

只是用熊猫

list(pd.DataFrame(listofstuff).melt().values)

这仅适用于列表列表

如果您有列表列表,则可能需要尝试以下方法

lists(pd.DataFrame(listofstuff).melt().apply(pd.Series).melt().values)

Just use pandas

list(pd.DataFrame(listofstuff).melt().values)

this only works for a list of lists

if you have a list of list of lists you might want to try something along the lines of

lists(pd.DataFrame(listofstuff).melt().apply(pd.Series).melt().values)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。