简体   繁体   中英

how to insert array in numpy using two variables

if I have array: a=[1,2,3] b=[7,8,9] I want to get new array c=[1,7,2,8,3,9], which is alternating arrangement between a and b. How to use np.insert on that case?

# solution 1
import numpy as np
a=[1, 2, 3]
b=[7, 8, 9]
list(np.transpose((a,b)).flatten())
# output [1, 7, 2, 8, 3, 9]

# solution 2
import operator
a=[1, 2, 3]
b=[7, 8, 9]
reduce(operator.concat, map(lambda x, y : [x, y], a, b))
# output [1, 7, 2, 8, 3, 9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM