简体   繁体   中英

making a 3d python array from three 1d arrays

i am new in python and have a basic question:

I have three lists :

a = [1, 2, 3]
b = [2, 4, 5]
c = [5, 7, 8]

What i want is an array that looks something like :

x = np.array([1,2,5],[2,4,7],[5,7,8])

is there some on-line python trick to do this?

np.vstack((np.array([1,2,3]), np.array([1,2,3]), np.array([1,2,3])))

甚至更简单

np.vstack(([1,2,3], [1,2,3], [1,2,3]))

Another simple way is using .T which transposes the matrix.

import numpy as np

a = [1, 2, 3]
b = [2, 4, 5]
c = [5, 7, 8]

np.array([a,b,c]).T

array([[1, 2, 5],
       [2, 4, 7],
       [3, 5, 8]])

试试zip(a, b, c) ,例如, x = np.array(*zip(a, b, c))官方文档

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