I have two numpy arrays:
我有两个numpy数组:
A = np.array([1, 3, 5, 7])
B = np.array([2, 4, 6, 8])
and I want to get the following from combining the two:
我想从这两者的结合中得出以下结论:
C = [1, 2, 3, 4, 5, 6, 7, 8]
I'm able to get something close by using zip
, but not quite what I'm looking for:
我可以通过使用zip来接近一些东西,但不是我想要的:
>>> zip(A, B)
[(1, 2), (3, 4), (5, 6), (7, 8)]
How do I combine the two numpy arrays element wise?
如何组合两个numpy数组元素?
I did a quick test of 50,000 elements in each array (100,000 combined elements). Here are the results:
我对每个数组中的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
Tested using Python 2.7, Windows 7 Enterprise 64-bit, Intel Core i7 2720QM @2.2 Ghz Sandy Bridge, 8 GB Mem
测试使用Python 2.7, Windows 7企业64位,Intel Core i7 2720QM @2.2 Ghz Sandy Bridge, 8gb Mem
5 个解决方案
#1
9
Use np.insert
:
使用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])
#2
5
You can also use slices :
你也可以使用切片:
C = np.empty((A.shape[0]*2), dtype=A.dtype)
C[0::2] = A
C[1::2] = B
#3
3
>>> import numpy as np
>>> A=np.array([1,3,5,7])
>>> B=np.array([2,4,6,8])
>>> C=np.dstack([A,B])
>>> D=C.reshape((1,8))[0]
>>> D
array([1, 2, 3, 4, 5, 6, 7, 8])
#4
1
Some answers suggested sorting, but since you want to combine them element-wise sorting won't achieve the same result.
一些回答建议进行排序,但是由于您希望将它们组合在一起,按元素排序不会得到相同的结果。
Here is one way to do it
这是一种方法。
C = []
for elem in zip(A, B):
C.extend(elem)
#5
0
You can try this:
你可以试试这个:
C = sorted(A.tolist() + B.tolist())
-
A.tolist()
will yield[1, 3, 5, 7]
- A.tolist()会产生[1,3,5,7]
-
B.tolist()
will yield[2, 4, 6, 8]
- B.tolist()将产生[2,4,6,8]
-
A.tolist() + B.tolist()
-[1, 3, 5, 7, 2, 4, 6, 8]
- A.tolist B.tolist()+()——(1、3、5、7、2,4,6,8]
-
sorted(...)
-[1, 2, 3, 4, 5, 6, 7, 8]
- 排序(…)-[1、2、3、4、5、6、7、8]
Without sorting:
没有排序:
C = [y for x in zip(A, B) for y in x]
#1
9
Use np.insert
:
使用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])
#2
5
You can also use slices :
你也可以使用切片:
C = np.empty((A.shape[0]*2), dtype=A.dtype)
C[0::2] = A
C[1::2] = B
#3
3
>>> import numpy as np
>>> A=np.array([1,3,5,7])
>>> B=np.array([2,4,6,8])
>>> C=np.dstack([A,B])
>>> D=C.reshape((1,8))[0]
>>> D
array([1, 2, 3, 4, 5, 6, 7, 8])
#4
1
Some answers suggested sorting, but since you want to combine them element-wise sorting won't achieve the same result.
一些回答建议进行排序,但是由于您希望将它们组合在一起,按元素排序不会得到相同的结果。
Here is one way to do it
这是一种方法。
C = []
for elem in zip(A, B):
C.extend(elem)
#5
0
You can try this:
你可以试试这个:
C = sorted(A.tolist() + B.tolist())
-
A.tolist()
will yield[1, 3, 5, 7]
- A.tolist()会产生[1,3,5,7]
-
B.tolist()
will yield[2, 4, 6, 8]
- B.tolist()将产生[2,4,6,8]
-
A.tolist() + B.tolist()
-[1, 3, 5, 7, 2, 4, 6, 8]
- A.tolist B.tolist()+()——(1、3、5、7、2,4,6,8]
-
sorted(...)
-[1, 2, 3, 4, 5, 6, 7, 8]
- 排序(…)-[1、2、3、4、5、6、7、8]
Without sorting:
没有排序:
C = [y for x in zip(A, B) for y in x]