[英]making a 3d python array from three 1d arrays
i am new in python and have a basic question:我是 python 新手,有一个基本问题:
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?是否有一些在线 python 技巧可以做到这一点?
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.另一种简单的方法是使用.T
转置矩阵。
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))
官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.