交织2 numpy的阵列 [英] Interweaving two numpy arrays

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

问题描述

假设以下阵列给出:

a = array([1,3,5])
b = array([2,4,6])

一个人怎么会有效地交织他们,这样一个得到第三阵列像这样

How would one interweave them efficiently so that one gets a third array like this

c = array([1,2,3,4,5,6])

可以假设长度()==长度(B)

推荐答案

我喜欢乔什的回答。我只是想补充一个更普通,平常,而且稍微详细的解决方案。我不知道这是更有效的。我希望他们将有类似的性能。

I like Josh's answer. I just wanted to add a more mundane, usual, and slightly more verbose solution. I don't know which is more efficient. I expect they will have similar performance.

import numpy as np
a = np.array([1,3,5])
b = np.array([2,4,6])

c = np.empty((a.size + b.size,), dtype=a.dtype)
c[0::2] = a
c[1::2] = b

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

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