Jan*_*oco 1 python numpy python-3.x
这是重现我的问题的示例:
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())
Run Code Online (Sandbox Code Playgroud)
当其中一个数组的项目少或多时,就会出现问题。
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]
Run Code Online (Sandbox Code Playgroud)
有谁知道如何正确展平列表,例如 b 和 c?
使用concatenate
np.concatenate(b)
Out[204]: array([1, 2, 3, 4, 6, 7, 8])
np.concatenate(c)
Out[205]: array([1, 2, 3, 4, 6])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
485 次 |
| 最近记录: |