Python 中元組的去重


在本文中,我們將學習一個 Python 程式,用於從元組中查詢唯一元素。

使用的方法

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

  • 使用 for 迴圈和 append() 函式

  • 使用 collections.Counter() 函式

  • 使用 set() 函式

元組是 Python 中用於儲存集合的一種不可變、無序的資料型別。它可以儲存多個值。列表和元組在許多方面相似,但列表的長度可變且是可變的,而元組的長度固定且是不可變的。

方法 1:使用 for 迴圈和 append() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個函式(uniqueElements),該函式返回傳遞給它的元組中的唯一元素。

  • 建立一個新的空列表,用於儲存元組的最終唯一元素。

  • 使用for 迴圈遍歷元組中的每個元素。

  • 使用if 條件語句以及 not in 運算子來檢查相應的元組元素是否不存在於上面定義的新列表中。

  • 如果條件為真,則使用append() 函式(將元素新增到列表的末尾)將元素追加到新列表中。

  • 使用tuple()函式將列表轉換為元組。

  • 返回結果元組。

  • 建立一個變數來儲存輸入元組。

  • 透過向其傳遞輸入元組來呼叫上面定義的uniqueElements函式,並列印結果元組。

示例

以下程式使用 for 迴圈和 append() 函式返回輸入元組中的唯一元素:

# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
   # creating a new list for storing unique elements
   newList = []
   # traversing through each element in a tuple
   for k in inputTuple:
      # appending that corresponding tuple element to the new list
      # if it is not present in it(i.e, storing all unique elements)
      if k not in newList:
         newList.append(k)
   # converting the list into a tuple
   resultTuple = tuple(newList)
   # returning the resultant tuple
   return resultTuple
# input tuple
inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))

輸出

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

The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)

方法 2:使用 collections.Counter() 函式

Counter() 函式(一個子類,用於計算可雜湊物件。在呼叫/呼叫時隱式地建立可迭代物件的雜湊表)

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 import 關鍵字從 collections 模組匯入Counter函式。

  • 建立一個函式(uniqueElements),該函式返回傳遞給它的元組中的唯一元素。

  • 將給定的元組作為引數傳遞給Counter()函式,該函式將所有唯一的元組元素及其頻率儲存為字典。

  • 使用keys() 和 tuple() 函式將上述頻率字典的鍵(唯一的元組元素)作為元組返回。

示例

以下程式使用 collections 模組的 Counter 函式返回輸入元組中的唯一元素:

# importing Counter from the collections module
from collections import Counter
# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
   #Getting all the unique tuple elements along with their frequencies
   frequency = Counter(inputTuple)
   # Returning all the unique tuple elements using the keys() function
   return tuple(frequency.keys())
# input tuple
inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))

輸出

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

The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)

方法 3:使用 set() 函式

set() 函式 - 建立一個集合物件。集合列表將以隨機順序出現,因為專案是無序的。它會刪除所有重複項)

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個函式(uniqueElements),該函式返回傳遞給它的元組中的唯一元素。

  • 使用 set() 函式返回元組中的所有唯一元素,並使用tuple()函式(建立一個元組)將其轉換為元組。

示例

以下程式使用 set() 函式返回輸入元組中的唯一元素:

# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
   # Getting all the unique elements of the tuple using the set() function
   resultUnique = set(inputTuple)
   # returning unique elements as a tuple using the tuple() function
   return tuple(resultUnique)
# input tuple
inputTuple = (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6)

# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))

輸出

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

The given Tuple is: (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (1, 3, 5, 6, 7, 8, 'tutorialspoint')

結論

在本文中,我們介紹瞭如何透過三種方法查詢元組的唯一元素。我們還學習瞭如何使用 Counter() 函式不僅查詢頻率,還查詢任何可迭代物件(例如列表、元組等)中的唯一元素。Counter() 方法是這三種方法中最有效的,因為它使用字典來儲存頻率。

更新於:2023年1月24日

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.