简化链式比较 [英] Simplify Chained Comparison
本文介绍了简化链式比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数值 x
,我需要检查它是否在 start
和 end
值之间,所以我写了以下陈述:
I have an integer value x
, and I need to check if it is between a start
and end
values, so I write the following statements:
if x >= start and x <= end:
# do stuff
此语句带有下划线,工具提示告诉我必须
This statement gets underlined, and the tooltip tells me that I must
简化链式比较
据我所知,这种比较和它们来的一样简单.我在这里错过了什么?
As far as I can tell, that comparison is about as simple as they come. What have I missed here?
推荐答案
在 Python 中你可以 "chain" 比较操作 意味着它们被和"在一起.在你的情况下,它会是这样的:
In Python you can "chain" comparison operations which just means they are "and"ed together. In your case, it'd be like this:
if start <= x <= end:
参考:https://docs.python.org/3/reference/表达式.html#comparisons
这篇关于简化链式比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文