Q:

Logical operators with lists

I have a problem:
I have two lists
Pipe_sizes = [15,15,22,15,32,45]
Flow_rate = [0.1,0.3,1,2,0.4,1.5]

I would like to use logical operators to change the list Pipe_size like:
if Flow_rate <= 0.2 then the pipe size is 15
if Flow_rate > 0.2 and <= 1 then the pipe size is 22
if Flow_rate > 1 and <=1.9  then the pipe size is 32
if Flow_rate > 1.9 then the pipe size is 45

How can I do it?

A:

Pipe_sizes is completely irrelevant for generating the ouput because all possible flow rate / pipe size combinations are covered in the list of conditions. So you can generate the 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) for rate in flow_rates]
print(pipe_sizes)

Output:

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

