在 Python 中检查数字是否不在范围内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36507957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:58:08  来源:igfitidea点击:

Checking if a number is not in range in Python

pythonrange

提问by What's in a Google Search

Ok, so I have Python code at the moment which does something like this:

好的,所以我现在有 Python 代码,它执行以下操作:

if plug in range(1, 5):
    print "The number spider has disappeared down the plughole"

But what I actually want to do is check if the number is notin range. I've googled and had a look at the Python docs but I can't find anything. Any ideas?

但我真正想做的是检查数字是否不在范围内。我用谷歌搜索并查看了 Python 文档,但我找不到任何东西。有任何想法吗?

Additional data: When running this code:

附加数据:运行此代码时:

if not plug in range(1, 5):
    print "The number spider has disappeared down the plughole"

I get the following error:

我收到以下错误:

Traceback (most recent call last):
    File "python", line 33, in <module>
IndexError: list assignment index out of range

I also tried:

我也试过:

if plug not in range(1,5):
     print "The number spider has disappeared down the plughole"

Which returned the same error.

返回相同的错误。

回答by Markus Meskanen

If your range has a stepof one, it's performance-wise much faster to use:

如果您的范围step为 1,则使用它的性能要快得多:

if not 1 <= plug < 5:

Than it would be to use the notmethod suggested by others:

比使用not其他人建议的方法:

if plug not in range(1, 5)

Proof:

证明:

>>> import timeit
>>> timeit.timeit('1 <= plug < 5', setup='plug=3')  # plug in range
0.053391717400628654
>>> timeit.timeit('1 <= plug < 5', setup='plug=12')  # plug not in range
0.05137874743129345
>>> timeit.timeit('plug not in r', setup='plug=3; r=range(1, 5)')  # plug in range
0.11037584743321105
>>> timeit.timeit('plug not in r', setup='plug=12; r=range(1, 5)')  # plug not in range
0.05579263413291358

And this is not even taking into account the time spent on creating the range.

这甚至没有考虑到创建range.

回答by Ebrahim Byagowi

This seems work as well:

这似乎也有效:

if not 2 < 3 < 4:
    print('3 is not between 2 and 4') # which it is, and you will not see this

if not 2 < 10 < 4:
    print('10 is not between 2 and 4')

Exact answer to the original question would be if not 1 <= plug < 5:I guess

if not 1 <= plug < 5:我猜原始问题的确切答案是

回答by Kunal Joshi

Use:

用:

if plug not in range(1,5):
     print "The number spider has disappeared down the plughole"

It will print given line whenever variable plug is out of range 1 to 5

每当变量插头超出范围 1 到 5 时,它将打印给定的行

回答by Yana Sosnovskaya

if (int(5.5) not in range(int(3.0), int(6.9))):
    print('False')
else:
    print('True')

value should type casting in integer, otherwise not in rangegives strange result.

value 应该在整数中进行类型转换,否则not in range会产生奇怪的结果。

回答by jusx

if not plug in range(1,5):
     #bla