Python程式:測試集合元素是否存在於列表中
在本文中,我們將學習如何在Python中檢查集合的任何元素是否存在於列表中。
使用的方法
使用any()函式
使用按位與(&)運算子
使用Counter()、filter()和lambda函式
示例
假設我們已經得到了一個輸入集合和一個輸入列表。我們現在將使用上述方法檢查任何輸入集合元素是否存在於輸入列表中。
輸入
inputSet = {4, 8, 1, 3, 5, 7} inputList = [7, 15, 20]
輸出
Checking whether any set element present in the input list: True
在上面的例子中,7 存在於集合和列表中,所以結果為True
方法一:使用any()函式
any()函式如果迭代器中的任何專案為真,則返回True,否則返回False。
語法
any(iterable)
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存輸入集合並列印給定的集合。
建立另一個變數來儲存輸入列表。
使用any()函式透過遍歷輸入集合並檢查當前元素是否存在於輸入列表中來檢查輸入列表中是否存在任何集合元素。
以布林值列印結果。
示例
下面的程式使用any()函式檢查輸入列表中是否存在任何輸入集合元素,如果存在則返回True,否則返回False。
# input set inputSet = {4, 8, 1, 3, 5, 7} # printing the input set print("Input set:\n", inputSet) # input list inputList = [7, 15, 20] # checking whether any set element is present in the input list using any() function result = any(i in inputSet for i in inputList) # printing the output print("Checking whether any set element present in the input list:", result)
輸出
執行後,上述程式將生成以下輸出:
Input set: {1, 3, 4, 5, 7, 8} Checking whether any set element present in the input list: True
方法二:使用按位與(&)運算子
按位與(&)運算子 − “&” 是一個按位運算子,用於比較數字(二進位制)。如果兩個位都為1,則將每個位設定為1。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用set()函式將給定的輸入轉換為集合。
使用&運算子(如果兩個位都為1,則將每個位設定為1)檢查輸入列表中是否存在任何集合元素,並使用bool()函式(返回給定物件的布林值)將結果轉換為布林值。
列印結果。
示例
下面的程式使用按位&運算子檢查輸入列表中是否存在任何輸入集合元素,如果存在則返回True,否則返回False。
# input set inputSet = {4, 8, 1, 3, 5, 7} # printing the input set print("Input set:\n", inputSet) # input list inputList = [9, 15, 20] # Convert the given list to set using the set() function inputListSet = set(inputList) # checking whether any set element present in the input list # using & operator(checks for common element) and converting to boolean result = bool(inputSet & inputListSet) # printing the output print("Checking whether any set element present in the input list:", result)
輸出
執行後,上述程式將生成以下輸出:
Input set: {1, 3, 4, 5, 7, 8} Checking whether any set element present in the input list: False
方法三:使用Counter()、filter()和lambda函式
filter()函式 − 使用一個函式來過濾指定的序列,該函式確定序列中每個元素是真還是假。
Counter()函式 − 一個子類,用於計數可雜湊的物件。它在被呼叫/呼叫時隱式地建立一個迭代物件的雜湊表。
lambda()函式
lambda函式是一個小的匿名函式。
lambda函式可以有無限/任意數量的引數,但只有一個表示式。
語法
lambda arguments : expression
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字從collections模組匯入Counter函式。
使用Counter()函式獲取所有輸入列表元素的頻率作為字典。
如果上述頻率字典中存在輸入集合元素,則使用filter函式過濾所有輸入集合元素。
如果存在任何公共元素,則過濾後的列表長度將大於1。
使用if條件語句檢查上述條件並相應地列印。
示例
下面的程式使用Counter()、filter()和lambda函式檢查輸入列表中是否存在任何輸入集合元素,如果存在則返回True,否則返回False。
# importing a Counter function from the collections module from collections import Counter # input set inputSet = {4, 8, 1, 3, 5, 7} # printing the input set print("Input set:\n", inputSet) # input list inputList = [7, 15, 20, 7] # getting the frequency of list elements using the Counter() function # Here it returns frequencies as a dictionary elements_freq = Counter(inputList) # Traversing in the input Set using the lambda function # Checking if the set element exists in the keys of the dictionary # Filtering all the elements which satisfy the above condition output = list(filter(lambda k: k in elements_freq.keys(), inputSet)) # Check if there are any filtered elements if(len(output) > 0): output = True # If no elements are common then the output will be False else: output = False # printing the output print("Checking whether any set element present in the input list:", output)
輸出
執行後,上述程式將生成以下輸出:
Input set: {1, 3, 4, 5, 7, 8} Checking whether any set element present in the input list: True
結論
在本文中,我們學習了三種不同的方法來確定集合是否包含來自列表的元素。我們還學習瞭如何使用set()函式將任何可迭代物件(如列表、元組或任何可迭代物件)轉換為集合,以及如何使用&運算子查詢給定的兩個集合中共同的元素。