在坚持行顺序的同时合并两个2D Numpy数组

时间:2014-12-04 17:17:03

标签: python arrays numpy

我有跟随2D numpy数组的列X和Y

[(476301.98163511883, 6178749.129456658)
 (477927.0510582989, 6178741.4760845825)
 (477918.79328165855, 6177112.834659822)]


[(476301.98163511883, 6175045.129456658)
 (477927.0510582989, 6175037.4760845825)
 (477918.79328165855, 6173408.834659822)]

我正在尝试将两者合并在一起,同时保留行顺序

结果如下所示:

[(476301.98163511883, 6178749.129456658), (476301.98163511883, 6175045.129456658)
 (477927.0510582989, 6178741.4760845825),  (477927.0510582989, 6175037.4760845825)
 (477918.79328165855, 6177112.834659822),  (477918.79328165855, 6173408.834659822)]

我目前使用这样的生成器工作,但我觉得这是 NOT 正确的方法。

    def generator_array():
        for value in both_arrays:
            yield value

    x = generator_array()

    feature_list = []

    try:
        while x:
            pt1 = next(x)
            pt2 = next(x)
            feature_list.append((pt1[0], pt1[1], pt2[0], pt2[1]))
    except StopIteration:
        pass

由于

3 个答案:

答案 0 :(得分:0)

我不太确定你问的是什么,因为你的代码没有产生你想要的结果,但会:

np.hstack((a,b))

工作,ab是哪两个数组?

答案 1 :(得分:0)

concatenate可行:

a=np.array([(476301.98163511883, 6178749.129456658), (477927.0510582989, 6178741.4760845825), (477918.79328165855, 6177112.834659822)])
b=np.array([(476301.98163511883, 6175045.129456658), (477927.0510582989, 6175037.4760845825), (477918.79328165855, 6173408.834659822)])

np.concatenate((a,b),axis=1)

array([[  476301.98163512,  6178749.12945666,   476301.98163512,
         6175045.12945666],
       [  477927.0510583 ,  6178741.47608458,   477927.0510583 ,
         6175037.47608458],
       [  477918.79328166,  6177112.83465982,   477918.79328166,
         6173408.83465982]])

答案 2 :(得分:0)

我可以通过以下方式让它工作

import numpy.lib.recfunctions as rfn

merged_array = rfn.merge_arrays((array1,array2), usemask = False)