如何检查Python数组中是否存在元素(相当于PHP in_array)? [英] How to check if an element exists in a Python array (Equivalent of PHP in_array)?
本文介绍了如何检查Python数组中是否存在元素(相当于PHP in_array)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手,我正在寻找一个标准函数,该函数可以告诉我数组中是否存在元素.我找到了index
方法,但是如果找不到该元素,它将引发异常.我只需要一些简单的函数,如果元素在数组中,则返回true
,否则返回false
.
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 index
method but it throws an exception if the element is not found. I just need some simple function that would return true
if the element is in the array or false
if not.
基本上等同于PHP in_array
.
Basically an equivalent to PHP in_array
.
推荐答案
>>> 1 in [0, 1, 2, 3, 4, 5]
True
这篇关于如何检查Python数组中是否存在元素(相当于PHP in_array)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文