简体   繁体   中英

multiple conditions in conditional

So i'm not exactly sure how to ask this question exactly but basically I want to see if any value between two variables is also in between two other variables. So for instance here is a sample of what code might look like of what I'm explaining

var1 = 0
var2 = 20
var3 = 5
var4 = 15
if var3 <= [any value in range var1 to var2] <= var4:
    do something

so that's basically it, but I'm not sure what would go in place of the brackets or if there is another way to do it. Sorry if there's an easy solution, I'm pretty tired. Thanks!

I'm surprised at the complexity of other answers. This should simply be:

def intersect(a, A, x, X):
  '''Return True if any number in range(a, A+1) is in range(x, X+1).'''
  return a <= X and x <= A

Note that intersection is symmetric, so intersect(a,b,x,y) == intersect(x,y,a,b) always holds.


All intersection possibilities:

   a...A
x..X

   a...A
 x..X

   a...A
  x..X

   a...A
   x..X

   a...A
    x..X

   a...A
     x..X

   a...A
      x..X

   a...A
       x..X

Which corresponds to the above function.


Finally, to make sure this is not different than John Kugelman's answer:

def their(a, b, x, y):
  return ((x <= a <= y or x <= b <= y) or (a <= x <= b or a <= y <= b))

def my(a, A, x, X):
  return a <= X and x <= A

from itertools import product
for x in product(range(5), repeat=4):
  if my(*x) != their(*x):
    if x[0] <= x[1] and x[2] <= x[3]:
      print('[{1}, {2}] and [{3}, {4}] intersect according to {0}.'
            .format('me' if my(*x) else 'them', *x))
    else:
      print('{} say it intersects, when input is incorrect.'
            .format('I' if my(*x) else 'They'))

Running this as python intersect.py | uniq -c python intersect.py | uniq -c outputs:

140 They say it intersects, when input is incorrect.

Let's use a bit of mathematical notation. So you have two number ranges, [ a , b ] and [ x , y ], where [ a , b ] represents the concept of "all numbers between a and b ".

One interpretation is you want to see if [ a , b ] is a subset of [ x , y ].

if a >= x and b <= y:
    ...

Another is that you want to see if [ a , b ] intersects [ x , y ] in any way. That happens when either of the two endpoints a or b is contained within [ x , y ], or vice versa.

if ((x <= a <= y or x <= b <= y) or
    (a <= x <= b or a <= y <= b)):
    ...

Always remember when you want to set a condition for some value, lets say 'a' between x and y, you can set a condition, a>x and a<y , which is what you want:

if var3 >= var1 and var3 <= var2 and var3 <=var4:
    do something

I am not 100% sure whether you want var3 be both var3 >= var1 and var3 <= var2 and var3 <=var4 or var3 be var3 >= var1 and var3 <= var2 or var3 <=var4 , please make changes as per your expected output.

Hope this helps, let me know if it doesn't work for you. This is a classic example but not the pythonic way though :)

You mean to do:

 for i in range(var1, var2+1):
    if var3 <= i <=var4:
      do something

Assuming that there isn't an ordering among the variables that's known ahead of time....

min34 = min(var3, var4)
max34 = max(var3, var4)

if ( (min34 < var1 && max34 > var1) || (min34 < var2 && max34 > var2) ) :
    do something

Use "<=" and ">=" if the edge of the range counts as "in between".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM