You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何使用比较运算符校验各分块内第三个数是否落在前两数区间

三元组区间校验Python实现

完整可运行代码

# 定义输入的三元组列表
triples = [[3, 3, 3], [4, 4, 4], [2, 4, 4], [4, 5, 1], [5, 5, 5], [2, 4, 3], [5, 5, 1], [4, 5, 4], [4, 5, 1], [2, 2, 2], [3, 3, 3], [5, 5, 5], [2, 2, 2], [3, 5, 3], [2, 2, 2], [1, 3, 1], [3, 3, 2], [2, 4, 4], [5, 5, 5], [3, 5, 3]]

# 校验规则:第三个数落在第一个数和第二个数的闭区间内
check_result = [t[0] <= t[2] <= t[1] for t in triples]

# 打印校验结果
print(check_result)

实现说明

  • 借助Python支持的链式比较语法直接完成区间判断,不需要拆分写两个比较条件再做逻辑与运算,代码简洁可读性强
  • 使用列表推导式遍历所有三元组,时间复杂度为O(n),执行效率高

运行输出结果

运行上述代码得到的每个三元组对应的校验结果如下:
[True, True, True, False, True, True, False, True, False, True, True, True, True, True, True, True, False, True, True, True]

内容的提问来源于stack exchange,提问作者Andri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 19:54:03