Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将两个数组连接为坐标对_Python 3.x_Concatenation_Numpy Ndarray - Fatal编程技术网

Python 3.x 将两个数组连接为坐标对

Python 3.x 将两个数组连接为坐标对,python-3.x,concatenation,numpy-ndarray,Python 3.x,Concatenation,Numpy Ndarray,我有两个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_ste

我有两个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)
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))
但结果是一个空的numpy数组。或者,我试过这样做:

    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)
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 cast前后的列表。

单向
meshgrid

x = np.array([1,2,3])
y = np.array([4,5,6])
np.array(np.meshgrid(x, y)).T.reshape(-1, 2)
将导致

array([[1, 4],
       [1, 5],
       [1, 6],
       [2, 4],
       [2, 5],
       [2, 6],
       [3, 4],
       [3, 5],
       [3, 6]])
meshgrid
对所有可能的组合进行配对,转换以将它们组合在一起,然后根据需要进行重塑