Numpy按行切割2d数组

时间:2017-10-05 14:16:31

标签: python numpy

我有一个像这样的2D数组:

a = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]

有没有办法以pythonic方式每行切割它们? 结果将是:

[[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[10,10]]

1 个答案:

答案 0 :(得分:2)

使用.T

a = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
a.T
array([[ 1,  1],
   [ 2,  2],
   [ 3,  3],
   [ 4,  4],
   [ 5,  5],
   [ 6,  6],
   [ 7,  7],
   [ 8,  8],
   [ 9,  9],
   [10, 10]])