【发布时间】:2020-08-04 02:05:41
【问题描述】:
我需要将我的第一个列表中的所有元素放置到第二个列表的第 k 个位置。其中k = 0,1,2... 和 n 是一个数字。目前我正在这样做(使用 numpy)
#create numpy array
positionList = np.array([])
positions = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
epochs = np.array([10, 11, 12])
for pos,epoch in zip(positions,epochs):
position = np.insert(pos,0,epoch)
if len(positionList) > 0:
positionList = np.concatenate((positionList,position))
else:
positionList = position
positionList = np.around(positionList,1).tolist()
#expected output [10, 1, 2, 3, 11, 4, 5, 6, 12, 7, 8, 9]
位置是二维的。我正在尝试使用 numpy 找到最有效的(时间和空间)方法。
注意:上面的代码确实有效。我只是想让它更有效率。
【问题讨论】:
-
你能更好地描述你想要做什么吗?
-
你基本上是想把两个数组像
a=[a1, a2, a3]; b=[b1, b2, b3]这样压缩成zipped = [a1, b1, a2, b2, a3, b3]吗? -
我希望它现在更清楚了。