我不知道如何在搜索引擎中搜索关键字。 我想制作一个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
答案 0 :(得分:0)
我将在这里使用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)
答案 1 :(得分:0)
您可以使用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]]