Store data generated in a cycle for a list

1

I have a problem, within a cycle for I have a variable that is changing value and I want to save each value generated within a list to be used in other operations. In my case, the variable that changes value is Asx , and I want to store each value that it generates in a list.

M = [2,5,6,8,7]  
fy = 4200  
h = 30  
b = 20  
d = 0.90*h  


for i in M:  
   Asx = (i*100000)/(0.81*fy*d)
    
asked by César Gtz 16.11.2017 в 01:49
source

1 answer

1

Cesar, the most practical thing is to convert Asx into a list, for which you must define it outside the cycle, for example as: Asx = [] or Asx = list() , then when iterating you add the values using your own method from the append() list. It would be something like this:

M = [2,5,6,8,7]  
fy = 4200  
h = 30  
b = 20  
d = 0.90*h  

Asx = []
for i in M:  
   Asx.append((i*100000)/(0.81*fy*d))

Or you can use list comprehension and make the code more compact in the following way:

Asx = [(i*100000)/(0.81*fy*d) for i in M]
    
answered by 16.11.2017 в 03:21