将 NumPy 数组转换为 Python 列表

发布于 2024-08-15 21:14:43 字数 99 浏览 2 评论 0原文

如何将 NumPy 数组转换为 Python 列表?

How do I convert a NumPy array into a Python List?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦醒时光 2024-08-22 21:14:43

使用 tolist()

>>> import numpy as np
>>> np.array([[1,2,3],[4,5,6]]).tolist()
[[1, 2, 3], [4, 5, 6]]

请注意,这会将值从它们可能具有的任何 numpy 类型(例如 np.int32 或 np.float32)转换为“最接近的兼容 Python 类型”(在列表中)。如果你想保留 numpy 数据类型,你可以在数组上调用 list() ,你最终会得到一个列表 numpy 标量。 (感谢 Mr_and_Mrs_D 在评论中指出这一点。)

Use tolist():

>>> import numpy as np
>>> np.array([[1,2,3],[4,5,6]]).tolist()
[[1, 2, 3], [4, 5, 6]]

Note that this converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the "nearest compatible Python type" (in a list). If you want to preserve the numpy data types, you could call list() on your array instead, and you'll end up with a list of numpy scalars. (Thanks to Mr_and_Mrs_D for pointing that out in a comment.)

巴黎夜雨 2024-08-22 21:14:43
c = np.array([[1,2,3],[4,5,6]])

list(c.flatten())
c = np.array([[1,2,3],[4,5,6]])

list(c.flatten())
一绘本一梦想 2024-08-22 21:14:43

如果 numpy 数组形状是二维的,则 numpy .tolist 方法会生成嵌套列表。

如果需要平面列表,可以使用下面的方法。

import numpy as np
from itertools import chain

a = [1,2,3,4,5,6,7,8,9]
print type(a), len(a), a
npa = np.asarray(a)
print type(npa), npa.shape, "\n", npa
npa = npa.reshape((3, 3))
print type(npa), npa.shape, "\n", npa
a = list(chain.from_iterable(npa))
print type(a), len(a), a`

The numpy .tolist method produces nested lists if the numpy array shape is 2D.

if flat lists are desired, the method below works.

import numpy as np
from itertools import chain

a = [1,2,3,4,5,6,7,8,9]
print type(a), len(a), a
npa = np.asarray(a)
print type(npa), npa.shape, "\n", npa
npa = npa.reshape((3, 3))
print type(npa), npa.shape, "\n", npa
a = list(chain.from_iterable(npa))
print type(a), len(a), a`
初见终念 2024-08-22 21:14:43

即使遇到嵌套数组,例如 pandas DataFrame,tolist() 也能正常工作;

my_list = [0,1,2,3,4,5,4,3,2,1,0]
my_dt = pd.DataFrame(my_list)
new_list = [i[0] for i in my_dt.values.tolist()]

print(type(my_list),type(my_dt),type(new_list))

tolist() works fine even if encountered a nested array, say a pandas DataFrame;

my_list = [0,1,2,3,4,5,4,3,2,1,0]
my_dt = pd.DataFrame(my_list)
new_list = [i[0] for i in my_dt.values.tolist()]

print(type(my_list),type(my_dt),type(new_list))
随风而去 2024-08-22 21:14:43

另一种选择

c = np.array([[1,2,3],[4,5,6]])

c.ravel()
#>> array([1, 2, 3, 4, 5, 6])

# or
c.ravel().tolist()
#>> [1, 2, 3, 4, 5, 6]

也有效。

Another option

c = np.array([[1,2,3],[4,5,6]])

c.ravel()
#>> array([1, 2, 3, 4, 5, 6])

# or
c.ravel().tolist()
#>> [1, 2, 3, 4, 5, 6]

also works.

愿得七秒忆 2024-08-22 21:14:43

将数组转换为列表的最简单方法是使用 numpy 包:

import numpy as np
#2d array to list
2d_array = np.array([[1,2,3],[8,9,10]])
2d_list = 2d_array.tolist()

要检查数据类型,可以使用以下命令:

type(object)

The easiest way to convert array to a list is using the numpy package:

import numpy as np
#2d array to list
2d_array = np.array([[1,2,3],[8,9,10]])
2d_list = 2d_array.tolist()

To check the data type, you can use the following:

type(object)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文