简体   繁体   English

python 检查两个值是否在两个值之间

[英]python check two values to be between two values

I have an issue, I want to verify if two values are between two values like:我有一个问题,我想验证两个值是否介于两个值之间,例如:

val1 = 23.04
val2 = 29.04

tobe1 = 24.04
tobe2 = 27.04
if tobe1, tobe2 in range(val1, val2):
   print("something")
>>> 3 < 5
True
>>> 3 < 4 < 6
True
>>> 3 < 7 < 6
False

range() method does something different than you expect. range()方法做的事情与你预期的不同。 Use simple < comparators.使用简单的<比较器。 You can replace my example values with variables.您可以用变量替换我的示例值。

This code should get you the required result此代码应该为您提供所需的结果

val1 = 23.04
val2 = 29.04

tobe1 = 24.04
tobe2 = 27.04

your_list = [tobe1, tobe2]
if all(val1 < x < val2 for x in (tobe1, tobe2)):
    print("something")

If you want ALL values in (tobe1, tobe2) to be within the val1 and val2, then use all如果您希望 (tobe1, tobe2) 中的所有值都在 val1 和 val2 内,则使用all

If you want ANY value in (tobe1, tobe2) to be within the val1 and val2, then use any .如果您希望 (tobe1, tobe2) 中的 ANY 值位于 val1 和 val2 内,请使用any

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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