3

Quite a simple question, but I have no clue on how to implement this.

Essentially:

>>> r = range(4,-1)
>>> 3 in r
False
>>> q = range(-1,4)
>>> 3 in q
True

As you can see, I have the same bounds, -1 and 4, and the same test value, so how do I say that '3' is between'-1' and '4' when I do not know the order that they are given to me in?

Tanishq dubey
  • 1,522
  • 7
  • 19
  • 42
  • possible duplicate of [how do i check if int is between the range of two numbers (tried various solutions before)?](http://stackoverflow.com/questions/13628791/how-do-i-check-if-int-is-between-the-range-of-two-numbers-tried-various-solutio) – Paul Oct 29 '14 at 06:40
  • From the above, this is a valid boolean in python: `1 <= r <= 10` – Paul Oct 29 '14 at 06:43
  • The bounds aren't the same - the stop value isn't included – John La Rooy Oct 29 '14 at 06:45
  • Python has `min()` and `max()`, which could be put to good use here. – Paul Oct 29 '14 at 06:54

7 Answers7

7

range doesn't do what you think it does.

It creates a list; it's not a numeric range (edit: actually it does in Python 3).

Just imagine a case, when the lowerEnd is -20000 and the upperEnd is +20000. Using the range( -20000, 20000 ) and comparing num in such a range() is a waste of both memory and CPU-power.

It is quite enough to compare a num against lowerEnd & upperEnd limit

You want to check:

num = 3
print(-1 < num < 4)
101
  • 8,514
  • 6
  • 43
  • 69
  • no it doesn't (return a list). The question is tagged Python3. range returns a "range object" – John La Rooy Oct 29 '14 at 06:47
  • 1
    You can chain the comparisons `-1 < num <=4`. There is an implicit `and`. This doesn't help if you don't know which bound is bigger/smaller though anyway – John La Rooy Oct 29 '14 at 06:48
6

Why not sort the bounds first?

r = range(*sorted((4, -1)))
q = range(*sorted((-1, 4)))
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Landed here searching something similar but for float dtypes (if float is between floats). Since floats' range can be infinite, it's best to just use sorted and access the values' index. `s = sorted(value1, value2)` and then use something like pandas Series between: `if pd.Series.between(4.21, s[0], s[1]):` – mrbTT Mar 12 '19 at 14:32
5

Why don't you try printing each step of your code snippet ? The code becomes self-explanatory to an extent. range() returns a list actually. So when this line is executed

r = range(4,-1)
print r 

[]   ## returned an empty list so which is why 3 in r returns False.

But when you execute like this

q = range(-1,4)
print q

[-1, 0, 1, 2, 3]  ## Returned a list with 3 in it so 3 in q returns True

Now you want to check falls in range then you can do like this

if -1<= 3 <= 4:
   print "Falls in the range"
else:
   print "not in range"

output:

Falls in the range
d-coder
  • 12,813
  • 4
  • 26
  • 36
0

Use a if statement to find the order

if c > d:
    c, d = d, c 

r = range(c, d)
DevLounge
  • 8,313
  • 3
  • 31
  • 44
Ravdeep
  • 730
  • 1
  • 8
  • 13
0

Maybe this?

r = list(xrange(4,-1,-1))
3 in r

True

q = list(xrange(-1,4,1))
3 in q

True
TinnyT
  • 147
  • 1
  • 1
  • 5
0

The syntax of the range builtin needs a little while to be grasped but when working with the interactive interpreter help(range) is always a helping hand...

Python 2.x

>>> print range(4,-1)
>>> print range(-1,4)

Pyton 3.x

>>> print(list(range(4,-1)))
>>> print(list(range(-1,4)))

but there is a problem (perhaps not with your specific issue) even if you add a step argument to the invocation of range (example for Python 2.x)

>>> print range(4, -1, -1)
>>> print range(-1,4)

as you can see, the two ranges are not the same: the start argument is in the list, the stop one is not.


You can think of it in these terms: "in a range, the stop argument means stop before".


gboffi
  • 22,939
  • 8
  • 54
  • 85
0

As answered by 101 and d-coder, your question to find a value between two other values should be constructed using < or <= operators, depending on what you want. Assuming that you know that r is a simple collection, like a list or tuple or range, but you don't know it's ordering, you could write:

>>> r = (-1,4,2,3,0)
>>> min(r) < 3 < max(r)
True
>>> min(r) < 2.5 < max(r)
True

The in operator on the other hand will test whether your value is one of the values in the list

>>> 3 in r
True
>>> 2.5 in r
False

Finally, your first example failed because you did not specify a negative step when trying to define 'r' as a decreasing sequence. Here's an example where I'll specify a step size of -2 as the 3rd argument of range():

>>> a = range(4,-1)
>>> print a
[]
>>> b = range(5,-1,-2)
>>> print b
[5, 3, 1]
>>> 3 in a
False
>>> 3 in b
True
Bill Olsen
  • 51
  • 5
  • So, you just took all the other answers and combined them into a new one? This does not bring anything to the table IMO. – DevLounge Mar 11 '17 at 20:30
  • This answer is actually useful and lays out clearly an approach that had only been hinted at in comments. It's also the only one provided that works for non-integers. – Mike Lewis Apr 13 '18 at 19:34