gpt4 book ai didi

python - 用 python 展平 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:55 24 4
gpt4 key购买 nike

这里是重现我的问题的例子:

a = np.array([[1,2], [3,4], [6,7]])
b = np.array([[1,2], [3,4], [6,7,8]])
c = np.array([[1,2], [3,4], [6]])
print(a.flatten())
print(b.flatten())
print(c.flatten())

当其中一个数组的项目少或多时,就会出现问题。

Output:
[1 2 3 4 6 7]
[list([1, 2]) list([3, 4]) list([6, 7, 8])] # Won't work
[list([1, 2]) list([3, 4]) list([6])] # Also won't work

How I want it:
[1 2 3 4 6 7]
[1 2 3 4 6 7 8]
[1 2 3 4 6]

有谁知道如何正确地展平列表,例如 b 和 c?

最佳答案

使用连接

np.concatenate(b)
Out[204]: array([1, 2, 3, 4, 6, 7, 8])
np.concatenate(c)
Out[205]: array([1, 2, 3, 4, 6])

关于python - 用 python 展平 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52970017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com