Issue
please help me, I’m stuck to assign lopping output to a variable
here’s my code
a = [0.5, 0.0, 1.2, 2.4, 0.1, 3.5]
for obs in a:
if obs > 0:
print('2')
elif obs < 0.1:
print('1')
the output is
2
1
2
2
2
2
I want to save that output in to a variable
Solution
You can create a variable out of the loop and save the value there or for a more "pythonic way" you can use list comprenhension
b = [1 if i <0.1 else 2 for i in a]
b is:
[2, 1, 2, 2, 2, 2]
Answered By – Andrew
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0