简体   繁体   中英

saving arrays in a for loop python

I have a code which is something like:

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

What I want to do, is save those two arrays I should get in the end, in two distinct arrays. Because then I don't only have an array of only 2 values, but more.

I want as output: to have two arrays, one: spec/arr[1] and the other: spec/arr[2]. but I want to store them in different arrays.

You can get a list of lists using a nested list comprehension:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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