Daniele
Daniele

Reputation: 23

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?

Upvotes: 0

Views: 148

Answers (6)

ziddarth
ziddarth

Reputation: 1916

If you don't mind modifying the inputs slightly numpy has some ways to bucket values like how you want:

import numpy as np
# pipe sizes for each of the different bins
pipe_bins = [15, 22, 32, 45]

# create the bins as monotonically increasing values 
# Each index i returned is such that 
# bins[i-1] <= x < bins[i] if bins is monotonically increasing
# I've changed to .000001 because you wanted x < 2
bins = np.array([0.200000001, 1.000000001, 1.9000000001])


input_flows = [0.1, 0.3, 1, 2, 0.4, 1.9]
# below list will contain the bins of each input flow
flow_in_bins = np.digitize(l, bins)
# now we can just map the flow bin to actual pipe size
result = map(lambda x: pipes[x], flow_in_bins)
print result
# result = [15, 22, 22, 45, 22, 45]

For more details about digitize check out the doc numpy.digitize

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54213

in golang, just because I need the practice (and hopefully to show off how much more concise python is....)

package main

import "fmt"

func getPipeSize(flowRate float32) (pipeSize int) {
    switch {
    case flowRate <= 0.2:
        pipeSize = 15
    case 0.2 < flowRate && flowRate <= 1.0:
        pipeSize = 22
    case 1.0 < flowRate && flowRate <= 1.9:
        pipeSize = 32
    case 1.9 < flowRate:
        pipeSize = 45
    }
    return
}

func main() {
    flow_rates := []float32{0.1, 0.3, 1, 2, 0.4, 1.5}
    pipe_sizes := make([]int, len(flow_rates))
    for i, flow_rate := range flow_rates {
        pipe_sizes[i] = getPipeSize(flow_rate)
    }
    fmt.Println(flow_rates)
    fmt.Println(pipe_sizes)
}

In Python:

def get_pipe_size(flow_rate):
    if flow_rate <= 0.2:
        pipe_size = 15
    elif 0.2 < flow_rate <= 1:
        pipe_size = 22
    elif 1 < flow_rate <= 1.9:
        pipe_size = 32
    else:
        pipe_size = 45
    return pipe_size

flow_rates = [0.1, 0.3, 1, 2, 0.4, 1.5]
pipe_sizes = [get_pipe_size(flow_rate) for flow_rate in flow_rates]

Upvotes: 0

mhawke
mhawke

Reputation: 87084

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]

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113988

Flow_rate = numpy.array([0.1,0.3,1,2,0.4,1.5])
pipe_sizes = numpy.zeros(len(FlowRate))

Flow_rate[Flow_rate <= 0.2] = 15
Flow_rate[Flow_rate > 0.2 & Flow_rate <= 0.4] = 15
...

might be good ... of coarse this isnt going to be very fast since each boolean iterates the whole list but it is fairly readable ...

Upvotes: 0

heinst
heinst

Reputation: 8786

I made a method to help you calculate the pipe sizes:

Flow_rate = [0.1,0.3,1,2,0.4,1.5]

def calculate_pipe_size(flow_rate):
    pipe_sizes = []
    for number in flow_rate:
        if number <= 0.2:
            pipe_sizes.append(15)
        if number > 0.2:
            pipe_sizes.append(22)
        if number > 1:
            pipe_sizes.append(32)
        if number > 1.9:
            pipe_sizes.append(45)
    return pipe_sizes

print calculate_pipe_size(Flow_rate)

Upvotes: 0

Ben
Ben

Reputation: 390

Enumerate over the values of Flow_rate and update Pipe_sizes accordingly as follows:

Pipe_sizes = [15,15,22,15,32,45]
Flow_rate = [0.1,0.3,1,2,0.4,1.5]
for i, flow_rate in enumerate(Flow_rate):
    if flow_rate <= .2:
        Pipe_sizes[i] = 15
    elif flow_rate <= 1:
        Pipe_sizes[i] = 22
    elif flow_rate <= 1.9:
        Pipe_sizes[i] = 32
    else:
        Pipe_sizes[i] = 45

Upvotes: 0

Related Questions