繁体   English   中英

使用给定的有序数据集启动二维数组

[英]initiate 2d array with given ordered datasets

我不知道如何在搜索引擎中搜索关键字。 我想制作一个2d数组,每列表示给定3个数组中的(x,y,z)。

x = [3,6,9,12]
y = [4,8,12,16]
z = [5,10,15,20]

对此:

[3,4,5],
[6,8,10],
[9,12,15],
[12,16,20]

我的代码如下所示,有没有更好的编写方法?

x = [3,6,9,12]
y = [4,8,12,16]
z = [5,10,15,20]
count=0
ans = []
for ind1 in range(4):
    ans.append([x[count], y[count], z[count]])
    count +=1

我将在这里使用numpy。

import numpy as np
xyz = np.zeros((4, 3))
x = [3,6,9,12]
y = [4,8,12,16]
z = [5,10,15,20]
xyz[:, 0] = np.reshape(x, -1)
xyz[:, 1] = np.reshape(y, -1)
xyz[:, 2] = np.reshape(z, -1)

你可以用zip

[ins] In [1]: x = [3,6,9,12]
         ...: y = [4,8,12,16]
         ...: z = [5,10,15,20]

[ins] In [2]: [list(x) for x in zip(x,y,z)]
Out[2]: [[3, 4, 5], [6, 8, 10], [9, 12, 15], [12, 16, 20]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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