【发布时间】:2020-11-23 08:59:30
【问题描述】:
我有两个 numpy 数组,我需要将它们组合成一个二维数组:每一行都必须是坐标对。例如,如果 numpy 数组是:
[1 2 3]
[a b c]
那么我的目标是:
[[1 a]
[1 b]
[1 c]
[2 a]
[2 b]
[2 c]
[3 a]
[3 b]
[3 c]]
我试过了:
import numpy as np
x1_start, x1_stop, x1_step = 88.5, 91.5, 0.2
x2_start, x2_stop, x2_step = 82, 90, 0.5
x1 = np.arange(x1_start, x1_stop, x1_step)
x2 = np.arange(x2_start, x2_stop, x2_step)
x1x2 = np.array([])
for k in range(len(x1)):
for h in range(len(x2)):
list = [x1[k], x2[h]]
np.append(x1x2, list ,0)
但结果是一个空的 numpy 数组。或者,我试过这个:
x1x2 = []
for k in range(len(x1)):
for h in range(len(x2)):
x1x2.append([x1[k],x2[h]])
print(type(x1x2))
np.asarray(x1x2)
print(type(x1x2))
该列表包含正确的数字,但是当我打印它的类型时,它在 np.array 转换之前和之后都是一个列表。
【问题讨论】:
标签: python-3.x concatenation numpy-ndarray