Check which range a number is within

Neil Macneale

I need to find which range a number, in this case value, is within. I cannot find any other way to set i to what I need other than this:

if value < -64:
  i = 0
elif value < -32:
  i = 1
elif value < -16:
  i = 2
elif value < -8:
  i = 3
elif value < -4:
  i = 4
elif value < -2:
  i = 5
elif value < -1:
  i = 6
elif value < -0.5:
  i = 7
elif value < 0:
  i = 8
elif value < 0.5:
  i = 9
elif value < 1:
  i = 10
elif value < 2:
  i = 11
elif value < 4:
  i = 12
elif value < 8:
  i = 13
elif value < 16:
  i = 14
elif value < 32:
  i = 15
elif value < 64:
  i = 16
else:
  i = 17

This is terrible code and I hate it. Is there any way I can do something like this?

ranges = [-64, -32, -16 ... 32, 64]
i = find_which_range(value, ranges)

Thanks!

Thierry Lathuille

Use bisect:

import bisect

ranges = [-64, -32, -16, -8, -4, -2, -1, -0.5, 0, 0.5, 1, 2, 4, 8, 16, 32, 64]

print(bisect.bisect(ranges, -65))
# 0

print(bisect.bisect(ranges, -64))
# 1

print(bisect.bisect(ranges, 63))
#16

print(bisect.bisect(ranges, 64))
# 17

bisect.bisect(l, value) returns the index at which value would have to be inserted in l, such that all values to the left are less than value. It will also be fast to search a large list, as it uses a bisection algorithm.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a number is within a range in shell

How to elegantly check if a number is within a range?

Check if a Negative number falls within a range

Check that number of received calls is within range with NSubstitute

How to check if a number is within range in activerecord?

Check if number is within the range of numbers of dataFrame

Check no dups in list and within number range

How to check if a number is within the range of the same number including float type

How to check which range a float variable is within, from database values

How to split a number in two, each of which lie within a range?

How to check if a number (float or integer) is within a range (0 - 100)

Elegant way to check if a number is within the range of a circular modulo set?

Excel - Way to check if number is within range in a single step?

Check if number in numpy array is within range specified in another array

Check if datetime is within range

Check if number is between a range

Check the number of digits or their range

Check if one Range is within another

Check if a value is within a range of numbers

Check value within range or not php

check if cell ref is within a range

Check to see if a value is within a range?

Check if object is within a cell range

Check if UTC time is within range

How to build an algorithm to find a combination, which summation is nearest to a number and its difference is within a range in c#

Detecting if a Number is within a Certain Range

Dataset Locate number within range

Excel Find Number Within a Range

loop to check if the multiples of a user defined number are even within a user defined range