垂直合并2个数组到元组Numpy [英] Merge 2 arrays vertical to tuple Numpy

查看:68
本文介绍了垂直合并2个数组到元组Numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很容易做到,但我似乎无法自己弄清楚.我有两个numpy数组,一个代表x值,另一个代表对应的y值:

it may be very easy to do but I can't seem to figure this out on my own. I have two numpy arrays, one representing the x value and the other one representing the corresponding y value:

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

是否有一种方法可以将这两个数组合并在一起(因此更容易读取每个x值的y值),就像一个tupple:

Is there a way to merge these two arrays together (so its easier to read the y value for each x value) like a tupple:

array = [(-1, -2), (0, -1), (1, 0), (2, 1)]

提前谢谢!

推荐答案

In [469]: x = np.array([-1, 0, 1, 2])
In [470]: y = np.array([-2, -1, 0, 1])

将它们加入2d数组:

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

转置该数组:

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

或使用标准的python zip-将数组视为列表

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)]

这篇关于垂直合并2个数组到元组Numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆