使用numpy将两个数组垂直合并到元组数组 [英] Merge two arrays vertically to array of tuples using numpy
本文介绍了使用numpy将两个数组垂直合并到元组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 numpy 数组:
I have two numpy arrays:
x = np.array([-1, 0, 1, 2])
y = np.array([-2, -1, 0, 1])
有没有办法像元组一样将这些数组合并在一起:
Is there a way to merge these arrays together like tupples:
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])
将它们加入二维数组:
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)]
这篇关于使用numpy将两个数组垂直合并到元组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文