python - Logical operators with lists -


i have problem: have 2 lists

pipe_sizes = [15,15,22,15,32,45] flow_rate = [0.1,0.3,1,2,0.4,1.5] 

i use logical operators change list pipe_size like:

if flow_rate <= 0.2 pipe size 15 if flow_rate > 0.2 , <= 1 pipe size 22 if flow_rate > 1 , <=1.9  pipe size 32 if flow_rate > 1.9 pipe size 45 

how can it?

pipe_sizes irrelevant generating ouput because possible flow rate / pipe size combinations covered in list of conditions. can generate result directly:

def flow_rate_to_size(rate):     if rate <= 0.2:         size = 15     elif 0.2 < rate <= 1:         size = 22     elif 1 < rate <= 1.9:         size = 32     else:         size = 45     return size  flow_rates = [0.1, 0.3, 1, 2, 0.4, 1.5] pipe_sizes = [flow_rate_to_size(rate) rate in flow_rates] print(pipe_sizes) 

output:

 [15, 22, 22, 45, 22, 32] 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -