交织两个 numpy 数组 [英] Interweaving two numpy arrays
本文介绍了交织两个 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])
可以假设length(a)==length(b)
.
推荐答案
我喜欢 Josh 的回答.我只是想添加一个更平凡、更平常、更冗长的解决方案.我不知道哪个更有效率.我希望它们会有类似的性能.
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
这篇关于交织两个 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文