简体   繁体   中英

Making 2 dimensional numpy array with two 1 dimensional array

I would like to convert 2 numpy arrays such as these ones:

a = [[1, 2, 3]]
b = [[100, 200, 300]]

to an array like below.

[[1, 100], [1, 200], [1, 300], [2, 100], [2, 200], [3, 300], [3, 100], [3, 200], [3, 300]]   

Is this possible in NumPy?

Thanks in advance.

(edited to clarify the point of this question.) I'm trying to find a numpy way of solution.

This is a job for meshgrid and stack :

a = np.array([ [1, 2, 3] ])
b = np.array([ [100, 200, 300] ])

print(np.stack(np.meshgrid(a, b)).T.reshape(-1,2))

The first creates a tuple of coordinate on the grid, the second stacks them. Then you just need to transpose and flatten.

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