General python function with many interval cases that returns specific values depending on interval -


i having problems writing function in python based on input x outputs y depending on in interval input x in. of code looks this

def getpowerlimit(x):                     if 0.009 <= x <= 380.2:                         return -39                     elif 380.2 < x <= 389.8:                         return -94                     elif 389.8 < x <= 390.2:                         return -39                     elif 390.2 < x <= 399.8:                         return -60                     elif 399.8 < x <= 410.2:                         return -39 

and needs proceed differing intervals around 12000. each differing return values means have alot of different cases. can not best approach problem wondering if there different , quicker way solve problem.

as suggested dlask's comment in question, can make use of bisect library:

boundaries = [0.009, 380.2, 389.8, 390.2, 399.8, 410.2] values = [none, -39, -94, -39, -60, -39, none]  # need import bisect values[bisect.bisect_left(boundaries, x)] 

assume can construct boundaries , values external sources csv.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -