在 for 循环中保存数组 python

saving arrays in a for loop python

我有一个类似这样的代码:

arr = [1,2]
spec = [4,10,5,45,78]
for i in range(len(arr)):
                       spec = spec/arr[i]

我想做的是,将我最终应该得到的那两个数组保存在两个不同的数组中。因为那时我不仅有一个只有 2 个值的数组,还有更多。

我想要作为输出:有两个数组,一个:spec/arr[1],另一个:spec/arr[2]。但我想将它们存储在不同的数组中。

您可以使用嵌套列表理解获取列表列表:

[[s/a for s in spec] for a in arr]

结果:[[4.0, 10.0, 5.0, 45.0, 78.0], [2.0, 5.0, 2.5, 22.5, 39.0]]