在Python 2 numpy的阵列交错排 [英] Interleave rows of two numpy arrays in Python

查看:370
本文介绍了在Python 2 numpy的阵列交错排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想交织相同大小的两个numpy的阵列的行。
我想出了这个解决方案。

I wanted to interleave the rows of two numpy arrays of the same size. I came up with this solution.

# A and B are same-shaped arrays
A = numpy.ones((4,3))
B = numpy.zeros_like(A)
C = numpy.array(zip(A[::1], B[::1])).reshape(A.shape[0]*2, A.shape[1])
print C

输出

[[ 1.  1.  1.]
 [ 0.  0.  0.]
 [ 1.  1.  1.]
 [ 0.  0.  0.]
 [ 1.  1.  1.]
 [ 0.  0.  0.]
 [ 1.  1.  1.]
 [ 0.  0.  0.]]

有一个更清洁,更快,更好,numpy的,唯一的办法?

Is there a cleaner, faster, better, numpy-only way?

推荐答案

这也许是更清楚一点做的:

It is maybe a bit clearer to do:

A = np.ones((4,3))
B = np.zeros_like(A)

C = np.empty((A.shape[0]+B.shape[0],A.shape[1]))

C[::2,:] = A
C[1::2,:] = B

和它可能是一个快一点为好,我猜。

and it's probably a bit faster as well, I'm guessing.

这篇关于在Python 2 numpy的阵列交错排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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