列表在 Python 中是一個至關重要的容器,因為它能夠將所有類型的數據元素存儲為一個集合。了解具體的列表操作對於能夠進行日常編程至關重要。本文將討論最基本的列表操作之一,即驗證列表中元素的存在。
方法 1:樸素方法
在 Naive 方法中,使用一個循環來遍歷整個元素列表,以驗證該元素是否是目標元素。這是確定列表中元素是否存在的最有效方法。
方法 2:用於
Python 是確定列表中是否存在元素的最常見方法。如果元素出現在列表中,此方法返回 True 如果元素不在集合中,則返回 False。使用這種檢查方法不需要排序列表。
代碼#1: 演示如何使用 Naive 方法以及 in 來檢查一個元素是否在列表中。
# First, we will initialize list
test_list1 = [ 1, 5, 2, 6, 3, 5 ]
print ("Check if 5 exists in list (using loop): ")
# Here, we will checking if 7 exists in list using loop
for K in test_list1:
if(K == 7) :
print ("Given element Exists")
else:
print ("Given element does not exists")
print ("Check if 5 exists in list (using in): ")
# Here, we will check if 5 exists in list using in
if (5 in test_list1):
print ("Given element Exists")
else:
print ("Given element does not exists")
輸出:
Check if 5 exists in list ( using loop ) :
Given element does not exists
Check if 5 exists in list ( using in ) :
Given element Exists
方法 3:在中使用 set() +
將列錶轉換為集合,然後使用 in 可能比僅使用 in 更有意義。然而,效率作為一個優勢也有一些缺點。其中一個問題是列表的顯示順序沒有得到維護。如果你選擇做一個列表,然後做一個新的,你需要利用額外的空間。另一個問題是 set 不允許重複,因此重複的元素將從列表中刪除。
方法 4:使用 sort() +平分 _left()
確定元素存在的傳統二分搜索方法意味着必須首先排序列表,從而不能保持元素的順序。平分 _left()將返回找到的元素的第一個實例。它與 C++ STL 中的下界()的工作方式類似。
注意:等分函數將只給出元素的位置,而不提供元素是否存在的細節。
代碼#2: 演示如何使用 set() + in 和 sort() +平分 _left()檢查列表中元素的存在。
from bisect import bisect_left as BL
from bisect import bisect as BS
# First, we will initialize list
test_list_set1 = [ 1, 5, 2, 6, 3, 5 ]
test_list_bisect1 = [ 1, 5, 2, 6, 3, 5 ]
print ("Checking if 5 exists in list (using set() + in): ")
# Check if 7 exists in list using set() + in
test_list_set1 = set(test_list_set1)
if 7 in test_list_set1:
print ("Given element Exists")
else:
print ("Given element does not exists")
print ("Checking if 7 exists in list (using sort() + bisect_left()): ")
# Check if 8 exists in list using sort() + bisect_left()
test_list_bisect1.sort()
if BL(test_list_bisect1, )!= BS(test_list_bisect1, 8):
print ("Given element Exists")
else:
print ("Given element does not exists")
輸出:
Checking if 5 exists in list (using set() + in):
Given element does not exists
Checking if 7 exists in list (using sort() + bisect_left()):
Given element does not exists
方法 5:使用計數()
我們可以利用內置的 Python List 方法 count()來驗證傳遞的元素是否存在於 List 中。如果我們傳遞的元素在列表中,那麼 count()技術將揭示它在整個列表中存在多少個實例。如果是正數,則表示列表中存在某個元素。
代碼#3: 解釋如何通過計數()來驗證列表中元素的存在。
# First, we will initialize list
test_list1 = [ 1, 5, 2, 6, 3, 5, 46, 2 ]
print ("Checking if 5 exists in list: ")
# Here we will calculate the number of times element exists in list
exist_count1 = test_list1.count(5)
# Now, we will check if it is more then 0
if exist_count1 > 0:
print("Yes, 5 exists in list")
else:
print("No, 5 does not exists in list")
#Checking for another number:
print ("Checking if 46 exists in list: ")
# Here we will calculate the number of times element exists in list
exist_count2 = test_list1.count(46)
# Now, we will check if it is more then 0
if exist_count2 > 0:
print("Yes, 46 exists in list")
else:
print("No, 46 does not exists in list")
輸出:
Checking if 5 exists in list:
Yes, 5 exists in list
Checking if 46 exists in list:
Yes, 46 exists in list
結論
在本教程中,我們討論了如何使用 Python 中的不同方法來檢查列表中是否存在給定的元素。
原創文章,作者:X465U,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128771.html