如何检查元素是否存在于 Python 数组中(相当于 PHP in_array)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14743156/
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
How to check if an element exists in a Python array (Equivalent of PHP in_array)?
提问by laurent
I'm new to Python and I'm looking for a standard function that would tell me if an element is present in an array. I found the indexmethod but it throws an exception if the element is not found. I just need some simple function that would return trueif the element is in the array or falseif not.
我是 Python 的新手,我正在寻找一个标准函数,它可以告诉我数组中是否存在元素。我找到了该index方法,但如果未找到该元素,则会引发异常。我只需要一些简单的函数,true如果元素在数组中,或者false不在数组中,它就会返回。
Basically an equivalent to PHP in_array.
基本上相当于 PHP in_array。
采纳答案by Ignacio Vazquez-Abrams
>>> 1 in [0, 1, 2, 3, 4, 5]
True

