Python程式:從字典值建立集合


在Python中,字典是一種被稱為關聯陣列的資料結構的實現。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。

在本文中,我們將學習如何在python中將字典值轉換為集合。

使用的方法

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

  • 使用生成器表示式和{}

  • 使用values()和set()函式

  • 使用Counter()函式

示例

假設我們已經獲取了一個輸入字典。我們現在將提取字典的所有值,然後將這些值轉換為集合。

輸入

inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

輸出

{10, 5, 15}

在上面的示例中,輸入字典的(5, 10, 15, 5)值被提取並轉換為集合。在轉換為集合時,所有重複項都被刪除。

因此,刪除所有重複值後,輸出為{10, 5, 15}

使用生成器表示式和{}

在這裡,我們使用生成器表示式來獲取所有值,而{}運算子負責刪除重複項並轉換為集合。

演算法(步驟)

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

  • 建立一個變數來儲存輸入字典

  • 列印輸入字典。

  • 遍歷輸入字典,並使用生成器表示式和{}將輸入字典的值轉換為集合。

  • 列印將輸入字典的值轉換為集合後的結果集合。

示例

以下程式使用生成器表示式和{}將輸入字典的值轉換為集合後返回一個集合:

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# {} operator is used to convert to set
# converting the values of the input dictionary to set 
resultantSet = {inputDict[i] for i in inputDict}

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

輸出

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

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

使用values()和set()函式

在此方法中,我們將使用Python的values()和set()函式的組合來從字典值轉換集合。這裡的set()函式建立一個集合物件。集合列表將以隨機順序出現,因為專案是無序的。它刪除所有重複項。dict.values()函式提供一個檢視物件,按插入順序顯示字典中所有值。

演算法(步驟)

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

  • 使用values()函式獲取輸入字典的所有值,並使用set()函式將其轉換為集合。set()函式還會刪除所有重複項。

  • 列印將輸入字典的值轉換為集合後的結果集合。

示例

以下程式使用values()和set()函式將輸入字典的值轉換為集合後返回一個集合:

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# getting the values of the input dictionary and converting them into a set 
# set() function also removes all duplicates 
resultantSet = set(inputDict.values())

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

輸出

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

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

使用Counter()函式

在此方法中,我們將使用Python中的counter()函式將字典值轉換為集合。這裡,Counter()函式是一個子類,用於計數可雜湊的物件。當呼叫/呼叫時,它隱式地建立一個可迭代物件的雜湊表。Counter()函式用於獲取輸入字典值的頻率。

演算法(步驟)

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

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

  • 建立一個變數來儲存輸入字典

  • 列印輸入字典。

  • 使用values()函式獲取輸入字典的值,並使用Counter()函式獲取這些值的頻率作為鍵值對。

  • 使用keys()函式從上述值的頻率中獲取鍵,並使用list()函式將其轉換為列表(將序列/可迭代物件轉換為列表)。

  • 使用set()函式將結果值列表轉換為集合。

  • 列印將輸入字典的值轉換為集合後的結果集合。

示例

以下程式使用Counter()函式將輸入字典的值轉換為集合後返回一個集合:

# importing Counter from collections module 
from collections import Counter

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# getting the frequency of values of input dictionary as key-value pairs 
valuesFrequency = Counter(inputDict.values())

# getting the keys from the frequency of the above values and converting them into a list 
resultantList = list(valuesFrequency.keys())

# converting the result values list into a set 
resultantSet = set(resultantList)

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

輸出

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

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

結論

本文介紹了三種不同的方法來構建字典值的集合。我們學習瞭如何使用內建方法(如{}運算子和set()函式)將資料轉換為集合。我們還學習瞭如何使用Counter()函式,在這種情況下,它與set()函式執行相同的函式,以從可迭代物件中獲取所有唯一值。

更新於:2023年8月18日

瀏覽量:910

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告