Jam*_*rtz 12 python arrays numpy python-2.7
我有两个numpy数组:
A = np.array([1, 3, 5, 7])
B = np.array([2, 4, 6, 8])
Run Code Online (Sandbox Code Playgroud)
我想从两者结合得到以下内容:
C = [1, 2, 3, 4, 5, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud)
我可以通过使用来获得一些东西zip,但不是我正在寻找的东西:
>>> zip(A, B)
[(1, 2), (3, 4), (5, 6), (7, 8)]
Run Code Online (Sandbox Code Playgroud)
我如何组合两个numpy数组元素?
我对每个阵列中的50,000个元素进行了快速测试(100,000个组合元素).结果如下:
User Ma3x: Time of execution: 0.0343832323429 Valid Array?: True
User mishik: Time of execution: 0.0439064509613 Valid Array?: True
User Jaime: Time of execution: 0.02767023558 Valid Array?: True
Run Code Online (Sandbox Code Playgroud)
使用Python 2.7,Windows 7企业版64位,英特尔酷睿i7 2720QM @ 2.2 Ghz Sandy Bridge,8 GB Mem进行测试
Jai*_*ime 10
用途np.insert:
>>> A = np.array([1, 3, 5, 7])
>>> B = np.array([2, 4, 6, 8])
>>> np.insert(B, np.arange(len(A)), A)
array([1, 2, 3, 4, 5, 6, 7, 8])
Run Code Online (Sandbox Code Playgroud)
你也可以使用切片:
C = np.empty((A.shape[0]*2), dtype=A.dtype)
C[0::2] = A
C[1::2] = B
Run Code Online (Sandbox Code Playgroud)