简体   繁体   English

如何从 python 中的列表创建二维数组?

[英]How to create a 2D array from a list of list in python?

I have a list of lists that looks something like this:我有一个看起来像这样的列表列表:

arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

I want to create a 2 by 2 array with the first 2 elements at the top and the last two at the bottom, something like:我想创建一个 2 x 2 数组,前 2 个元素位于顶部,后两个元素位于底部,如下所示:

[1, 2, 3], [4, 5, 6]
[7, 8, 9], [10, 11, 12]

What I tried so far was reshape using:到目前为止,我尝试的是使用以下方法重塑:

np.array(arr).reshape(2,2)

However whenever I do that I get a ValueError: total size of new array must be unchanged What exactly am I missing or how can I get this to work?但是,每当我这样做时,我都会得到一个ValueError:新数组的总大小必须保持不变我到底错过了什么或者我怎样才能让它工作?

Try using a list comprehension:尝试使用list推导:

nparr = np.array([arr[i:i + 2] for i in range(0, len(arr), 2)]
print(nparr)

Output: Output:

[[[1 2 3] [4 5 6]]
 [[7 8 9] [10 11 12]]]

You could go for something like this, which reshapes the numpy array.你可以用 go 来做这样的事情,它会reshapes numpy数组。

import numpy as np
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
out = np.reshape(sum([], arr), (-1, 3))
print(out)

Output: Output:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

I think what you want is not exactly possible like this but this could satisfy your needs:我认为您想要的并不是完全可能的,但这可以满足您的需求:

arr2 = [arr[:2], arr[2:]]
np.array(arr2)

This returns something like this:这将返回如下内容:

[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

Note that arr has a total of 12 elements.请注意, arr共有 12 个元素。 You can not arrange 12 elements in a 2,2 manner using reshape.您不能使用 reshape 以 2,2 方式排列 12 个元素。

This is a possible solution for you这是您可能的解决方案

 np.array(arr).reshape(2, 6)

Output Output

array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]])

There seems to be some confusion as to what you want:你想要什么似乎有些混乱:

In [93]: alist = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

Making an array from the list:从列表中创建一个数组:

In [94]: np.array(alist)
Out[94]: 
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
In [95]: _.shape
Out[95]: (4, 3)

That's a total of 12 elements.总共有 12 个元素。 reshape has to match that: reshape必须匹配:

In [96]: np.array(alist).reshape(2,2,3)
Out[96]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
In [97]: _.shape
Out[97]: (2, 2, 3)

The accepted answer does the same thing:接受的答案做同样的事情:

In [99]: np.array([alist[i:i + 2] for i in range(0, len(alist), 2)])
Out[99]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In nested list form:以嵌套列表形式:

In [100]: [alist[i:i + 2] for i in range(0, len(alist), 2)]
Out[100]: [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]

When defining/displaying 2 and 3d arrays, the [] are more important than the line breaks and spacing.在定义/显示 2 和 3d arrays 时,[] 比换行符和间距更重要。

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

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