Python程式查詢最常見的元素集


在Python中,集合是一個無序的唯一元素集合,用{}表示。它允許高效的成員測試並消除重複值,使其適用於刪除重複項或檢查集合之間共同元素等任務。

在本文中,我們將學習一個Python程式來查詢最常見的元素集。

使用的方法

以下是完成此任務的各種方法

  • 使用for迴圈和set.intersection()函式

  • 使用列表推導式、max()和intersection()函式

演示

假設我們已經獲取了一個包含集合的**輸入列表**。我們現在將使用上述方法獲取最常見的元素。

輸入

inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
argumentSet = {5, 4, 9, 1}

輸出

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

這裡集合{8,1,4,5}包含給定引數集合{5,4,9,1}中最常見的元素,即(4,5,1)。

演算法(步驟)

以下是執行所需任務需要遵循的演算法/步驟。

  • 建立一個變數來儲存**集合的輸入列表**。

  • 列印輸入列表。

  • 建立另一個變數來儲存**輸入引數集**。

  • 使用**set()**函式建立一個空集(建立一個集合物件。集合列表將以隨機順序出現,因為專案是無序的。它刪除所有重複項)來儲存結果集。

  • 將最大長度初始化為0。

  • 使用**for迴圈**遍歷集合輸入列表的每個元素。

  • 使用**if條件**語句檢查公共元素的數量是否大於最大長度變數。

  • 如果為真,則將最大長度分配給這些公共元素的數量。

  • 將當前集合分配為結果。

  • 列印包含集合輸入列表中最常見元素的結果集。

示例1:使用for迴圈和set.intersection()函式

在此示例中,我們將使用for迴圈和set.intersection()函式來查詢給定集合中最常見的元素。

**set.intersection()函式:**intersection()方法返回一個包含兩個或多個集合之間相似性的集合。

這意味著僅在兩個集合中都存在(如果比較兩個以上集合,則在所有集合中都存在)的專案包含在返回的集合中。

**len()函式:**len()方法返回物件中的專案數。當物件是字串時,len()函式返回字串中的字元數。

示例

以下程式使用for迴圈和set.intersection()函式返回一個包含輸入集合中最常見元素的集合

# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# creating an empty set for storing resultant set
resultantSet = set()
# initiliazing maximum length as 0
maxLength = 0
# traversing through the input list
for i in inputList:
    # Checking the common elements is greater than the maximum length variable
    if len(i.intersection(argumentSet)) > maxLength:
        # If the above condition is true then add these common elements to the max length
        maxLength = len(i.intersection(argumentSet))
        # assigning a current element to the resultant set
        resultantSet = i
# printing resultant Set
print("The Most common elements in the input set are:", resultantSet)

輸出

執行後,上述程式將生成以下輸出

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

示例2:使用列表推導式、max()和intersection()函式

在此方法中,我們將學習如何使用列表推導式、max()和intersection()函式的簡單組合來查詢最常見的元素集。

列表推導式

當您希望根據現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

max()函式

**max()**方法返回可迭代物件中值最高的專案/最大數以下程式使用列表推導式、max()和intersection()函式返回一個包含輸入集合中最常見元素的集合

# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# Get the most number of common elements from the given list of sets
maxLength = max(len(i.intersection(argumentSet)) for i in inputList)
# Getting the set with the above number of common elements
resultantSet = [i for i in inputList if len(i.intersection(argumentSet)) == maxLength][0]
# printing resultant Set
print("Most common elements in input set are:", resultantSet)

輸出

執行後,上述程式將生成以下輸出:

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

結論

在本文中,我們學習了兩種不同的方法來從給定的集合列表中查詢最常見的元素集。此外,我們學習瞭如何使用intersection函式從給定的兩個集合中提取公共元素。

更新於:2023年8月17日

426次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.