python - Mapping a range of integers to a single integer -
i have function receives integer input , depending on range input lies in, assigns difficulty value. know can done using if else loops. wondering whether there more efficient/cleaner way it.
i tried this
time_rating_key ={ range(0,46):1, range(46,91):2, range(91,136):3, range(136,201):4, range(201,10800):5, } but found out can utilize range key in dict(right?). there improve way this?
the simplest way write short function:
def convert(n, difficulties=[0, 46, 91, 136, 201]): if n < difficulties[0]: raise valueerror difficulty, end in enumerate(difficulties): if n < end: homecoming difficulty else: homecoming len(difficulties) examples:
>>> convert(32) 1 >>> convert(68) 2 >>> convert(150) 4 >>> convert(250) 5 as side note: can utilize range dictionary key in python 3.x, not straight in 2.x (because range returns list). do:
time_rating_key = {tuple(range(0, 46)): 1, ...} however won't much help!
python django dictionary map
No comments:
Post a Comment