📌  相关文章
📜  检查Python列表中是否存在元素

📅  最后修改于: 2022-05-13 01:54:50.476000             🧑  作者: Mango

检查Python列表中是否存在元素

List 是Python中的一个重要容器,就像将所有数据类型的元素存储为一个集合一样。对于日常编程,某些列表操作的知识是必要的。本文讨论了检查列表中元素是否存在的方法的基本列表操作之一。

方法一:朴素方法

在 Naive 方法中,很容易使用循环遍历所有元素来检查目标元素的存在。这是检查列表中元素是否存在的最简单方法。

方法二:使用 in

Python是检查列表中是否存在元素的最常规方法。如果列表中存在元素,则此特定方式返回 True,如果列表中不存在元素,则返回 False。列表不需要排序来练习这种检查方法。

代码 #1:演示使用 Naive 方法和在 .

Python3
# Python code to demonstrate
# checking of element existence
# using loops and in
 
# Initializing list
test_list = [ 1, 6, 3, 5, 3, 4 ]
 
print("Checking if 4 exists in list ( using loop ) : ")
 
# Checking if 4 exists in list
# using loop
for i in test_list:
    if(i == 4) :
        print ("Element Exists")
 
print("Checking if 4 exists in list ( using in ) : ")
 
# Checking if 4 exists in list
# using in
if (4 in test_list):
    print ("Element Exists")


Python3
# Python code to demonstrate
# checking of element existence
# using set() + in
# using sort() + bisect_left()
from bisect import bisect_left ,bisect
 
# Initializing list
test_list_set = [ 1, 6, 3, 5, 3, 4 ]
test_list_bisect = [ 1, 6, 3, 5, 3, 4 ]
 
print("Checking if 4 exists in list ( using set() + in) : ")
 
# Checking if 4 exists in list
# using set() + in
test_list_set = set(test_list_set)
if 4 in test_list_set :
    print ("Element Exists")
 
print("Checking if 4 exists in list ( using sort() + bisect_left() ) : ")
 
# Checking if 4 exists in list
# using sort() + bisect_left()
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4)!=bisect(test_list_bisect, 4):
    print ("Element Exists")
else:
    print("Element doesnt exist")


Python3
"""
Python code to demonstrate
checking of element existence
using List count() method
"""
 
# Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
 
print("Checking if 15 exists in list")
 
# number of times element exists in list
exist_count = test_list.count(15)
 
# checking if it is more then 0
if exist_count > 0:
    print("Yes, 15 exists in list")
else:
    print("No, 15 does not exists in list")


输出 :

Checking if 4 exists in list ( using loop ) : 
Element Exists
Checking if 4 exists in list ( using in ) : 
Element Exists

方法 3:使用set() + in

将列表转换为集合,然后使用in可能比仅使用 in 更有效。但是具有加号的效率也有一定的负面影响。其中之一是不保留列表的顺序,如果您选择为其获取新列表,则需要使用额外的空间。另一个缺点是 set 不允许重复,因此重复的元素将从原始列表中删除。

方法 4:使用sort() + bisect_left()

测试元素存在的传统二进制搜索方式,因此必须首先对列表进行排序,因此不保留元素排序。 bisect_left() 返回要找到的元素的第一次出现,并且与 C++ STL 中的 lower_bound() 类似。

代码 #2:演示使用 set() + in 和 sort() + bisect_left() 检查列表中元素的存在。

Python3

# Python code to demonstrate
# checking of element existence
# using set() + in
# using sort() + bisect_left()
from bisect import bisect_left ,bisect
 
# Initializing list
test_list_set = [ 1, 6, 3, 5, 3, 4 ]
test_list_bisect = [ 1, 6, 3, 5, 3, 4 ]
 
print("Checking if 4 exists in list ( using set() + in) : ")
 
# Checking if 4 exists in list
# using set() + in
test_list_set = set(test_list_set)
if 4 in test_list_set :
    print ("Element Exists")
 
print("Checking if 4 exists in list ( using sort() + bisect_left() ) : ")
 
# Checking if 4 exists in list
# using sort() + bisect_left()
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4)!=bisect(test_list_bisect, 4):
    print ("Element Exists")
else:
    print("Element doesnt exist")

方法 5:使用count()

我们可以使用Python内置的 List 方法 count() 来检查传递的元素是否存在于 List 中。如果传递的元素存在于列表中,count() 方法将显示它在整个列表中出现的次数。如果它是一个非零的正数,则表示列表中存在一个元素。

代码 #3 :演示使用 count() 检查列表中元素的存在。

Python3

"""
Python code to demonstrate
checking of element existence
using List count() method
"""
 
# Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
 
print("Checking if 15 exists in list")
 
# number of times element exists in list
exist_count = test_list.count(15)
 
# checking if it is more then 0
if exist_count > 0:
    print("Yes, 15 exists in list")
else:
    print("No, 15 does not exists in list")