Python程式:查詢在範圍內但不在集合中的數字
在Python中,我們可以使用not運算子或減法運算,以及計數器函式來查詢在範圍內但不在集合中的數字。
Python的集合是無序元素的組合。集合的元素必須是唯一的、不可變的,並且集合會自動消除重複項。集合是可變的,建立後可以修改。
示例
假設我們已經獲得了一個輸入集合和範圍
輸入
inputSet = {3, 10, 2, 11, 15, 4, 1} lowIndex, highIndex = 0, 8
輸出
[0, 5, 6, 7]
這裡0, 5, 6, 7是在給定範圍內但不在集合中的元素。
使用for迴圈和not運算子
range()函式返回一個數字序列,從0開始,預設遞增1,直到給定數字之前停止。
not運算子(一個邏輯運算子,如果語句為假則返回True,否則返回False)
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入集合。
列印輸入集合。
建立兩個獨立的變數來儲存輸入的下限和上限索引。
建立一個空列表來儲存不在輸入集合中的結果數字。
使用for迴圈遍歷使用range()函式生成的輸入下限和上限索引之間的範圍。
使用if條件語句檢查當前索引是否不在輸入集合中,使用not運算子。
使用append()函式(將元素新增到列表的末尾)將當前元素(索引)新增到結果列表中。
列印不在給定輸入範圍內的集合中的元素的結果列表。
示例
以下程式使用for迴圈和not運算子返回給定輸入範圍中不在輸入集合中的數字列表:
# input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # resultant list for storing numbers, not in the set resultantList = [] # travsersing in a range from input low and high index for i in range(lowIndex, highIndex): # checking whether the current index does not exist in the input set if i not in inputSet: # appending the current element to the resultant list resultantList.append(i) # printing the resultant list of elements not in a set # within the specified range print("Resultant list of elements not in a set within the specified range:\n", resultantList)
輸出
執行上述程式將生成以下輸出:
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
使用“-”運算子
以下程式使用“-”運算子返回給定輸入範圍中不在輸入集合中的數字列表:
示例
# input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # Converting the numbers in the range to set # Subtracting this set from the given input set resultantList = list(set(range(lowIndex, highIndex)) - inputSet) # printing the resultant list of elements not in a set print("Resultant list of elements not in a set within the specified range:\n", resultantList)
輸出
執行上述程式將生成以下輸出:
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
使用Counter()函式
Counter()函式 - 一個子類,用於計數可雜湊的物件。呼叫/呼叫時,它會隱式地建立一個可迭代物件的雜湊表。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字從collections模組匯入Counter函式。
使用Counter()函式獲取輸入集合元素的頻率,以鍵值對的形式。
建立兩個獨立的變數來儲存輸入的下限和上限索引。
建立一個空列表來儲存不在輸入集合中的結果數字。
使用for迴圈遍歷輸入下限和上限索引之間的範圍。
使用if條件語句檢查當前元素是否不在集合頻率的鍵中,使用keys()函式。
使用append()函式將當前元素新增到結果列表中。
列印不在給定輸入範圍內的集合中的元素的結果列表。
示例
以下程式使用Counter()函式返回給定輸入範圍中不在輸入集合中的數字列表:
# importing Counter from collections from collections import Counter # input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # getting the frequency of elements of the input set setFrequency = Counter(inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # resultant list for storing numbers, not in the set resultantList = [] # travsersing in a range from input low and high index for i in range(lowIndex, highIndex): # checking whether the current element is not in keys of the set frequency if i not in setFrequency.keys(): # appending current item to the resultant list resultantList.append(i) print("Resultant list of elements not in a set within the specified range:\n", resultantList)
輸出
執行上述程式將生成以下輸出:
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
結論
在本文中,我們學習瞭如何使用三種不同的方法查詢在範圍內但不在集合中的數字。此外,我們學習瞭如何使用range()函式獲取指定索引之間的數字範圍。最後,我們學習瞭如何利用Counter()函式快速有效地(O(1)時間複雜度)確定給定集合中是否存在元素。