user3613025
user3613025

Reputation: 383

Turning a function with multiple if else statement into a dictionary

So I currently have a function that outputs sound barrier for the input height.

def soundbarier (y):   
    if 0 < y < 1524:
        S = 340.3
    elif 1524 < y < 3048:
        S = 334.4
    elif 3048 < y < 4572:
        S = 328.4
    elif 4572 < y < 6096:
        S = 322.2
    elif 6096 < y < 7620:
        S = 316.0
    elif 7620 < y < 9144:
        S = 309.6
    elif 9144 < y < 10668:
        S = 303.6
    elif 10668 < y < 12192:
        S = 295.4
    else:
        S = 294.5
    return (float(S))

I want to shorten it using a dictionary but I can't get it to work.

def SOUND(y):
    return {
    0 < float(y) < 1524: 340.3,
    1524 < y < 3048: 334.4,
    3048 < y < 4572: 328.4,
    4572 < y < 6096: 322.2,
    6096 < y < 7620: 316.0,
    7620 < y < 9144: 309.6,
    9144 < y < 10668: 303.6,
    10668 < y < 12192: 295.4
    }.get(y, 9)

print(SOUND(1500))

I don't really want any default values. How could I make it work? I basically need the function to output S for a certain Y within a range.

Upvotes: 3

Views: 817

Answers (4)

Lingster
Lingster

Reputation: 1087

The problem with your code is that python will evaluate the keys in your dictionary to True/False.

How about creating a mapping table(ie a dictionary of tuples to values) like this:

sound_barrier = { (0, 1524): 340.3, (1524, 3048): 334.4, (3048, 4572): 328.4, ... }
def get_mapping(table, value):
    for k in table: 
        if k[0] < value < k[1]:
            return table[k]
get_mapping(sound_barrier, 2000)

Upvotes: 0

Evgeny
Evgeny

Reputation: 3274

There is no built-in syntax for this, but you can implement it efficiently with 'bisect', which finds location of a number in a list for you.

def soundbar(y):
  barrier_dict = {
    1524: 340.0,
    3048: 334.4,
    4572: 328.4,
    float('inf'): 0
  }
  height, barrier = zip(*sorted(barrier_dict.items()))
  return barrier[bisect(height, y)]

print 1000, '->', soundbar(1000)
print 2000, '->', soundbar(2000)
print 2500, '->', soundbar(2500)
print 10000, '->', soundbar(10000)

outputs:

1000 -> 340.0
2000 -> 334.4
2500 -> 334.4
10000 -> 0

Upvotes: 2

cs95
cs95

Reputation: 402932

Mathemagic by @Tim.

consts = [340.3, 334.4, 328.4, 322.2, 316.0, 309.6, 303.6, 295.4, 295.5]

def soundbarrier(y):
    return consts[
        -1 if (not (0 < y < 12192) or y % 1524 == 0) else y // 1524
    ]

In [1]: soundbarrier(1500)
Out[1]: 340.3

In [2]: soundbarrier(10000)
Out[2]: 303.6

In [3]: soundbarrier(100)
Out[3]: 340.3

In [4]: soundbarrier(1524)
Out[4]: 295.5

Upvotes: 2

jpp
jpp

Reputation: 164783

One general solution is to use numpy.digitize combined with a dictionary.

Note this doesn't deal with edge cases where your input is on a boundary, in which case floating point approximations may need careful attention.

import numpy

def SOUND(y):

    bins = np.array([0, 1524, 3048, 4572, 6096, 7620, 9144, 10668, 12192])
    values = np.array([340.3, 334.4, 328.4, 322.2, 316.0, 309.6, 303.6, 295.4])

    d = dict(enumerate(values, 1))

    return d[int(np.digitize(y, bins))]

SOUND(7200)  # 316.0

Upvotes: 1

Related Questions