Merge 2 arrays vertical to tuple Numpy

前端 未结 1 431
北海茫月
北海茫月 2020-12-13 14:58

I have two numpy arrays: one representing the x-values and one representing the y-values:

x = np.array([-1, 0, 1, 2])
y = np.array([-2, -1, 0, 1])


        
相关标签:
1条回答
  • 2020-12-13 15:34
    In [469]: x = np.array([-1, 0, 1, 2])
    In [470]: y = np.array([-2, -1, 0, 1])
    

    join them into 2d array:

    In [471]: np.array((x,y))
    Out[471]: 
    array([[-1,  0,  1,  2],
           [-2, -1,  0,  1]])
    

    transpose that array:

    In [472]: np.array((x,y)).T
    Out[472]: 
    array([[-1, -2],
           [ 0, -1],
           [ 1,  0],
           [ 2,  1]])
    

    or use the standard Python zip - this treats the arrays as lists

    In [474]: zip(x,y)   # list(zip in py3
    Out[474]: [(-1, -2), (0, -1), (1, 0), (2, 1)]
    
    0 讨论(0)
提交回复
热议问题