如何在Python中建立集合字典?
在本文中,我們將學習如何在Python中建立集合字典。
使用方法
以下是完成此任務的各種方法:
使用樸素方法
使用defaultdict()方法
使用setdefault()方法
方法1:使用樸素方法
在此方法中,我們透過將集合作為值傳遞給鍵來建立一個集合字典。
語法
{ ‘Key’: Set 1, ‘Key’:Set 2,…………..,’Key’: Set n}
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存字典,該字典的值為不包含重複元素的集合(即集合字典)。
列印輸入的集合字典。
示例
以下程式使用樸素方法在python中建立一個集合字典(不包含重複項):
# creating a dictionary containing the values as set
# without duplicate elements
# (dictionary of sets)
inputDict = {'Employ ID': {10, 11, 12, 14, 15},
'Employ Age': {25, 30, 40, 35, 28}}
# printing the input dictionary of sets
print(inputDict)
輸出
執行上述程式將生成以下輸出:
{'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}}
建立包含重複項的集合
通常,python中的**集合**不允許**重複**,即它會移除所有重複的元素。
示例
以下示例顯示python集合不允許重複。
以下程式使用樸素方法在python中建立一個集合字典(包含重複項):
# creating a dictionary containing the values as set
# with duplicates elements
# the set does not allow duplicates
inputDict = {'Employ ID': {10, 11, 12, 13, 13, 14, 15, 10, 12, 11},
'Employ Age': {25, 30, 30, 40, 25, 35, 40, 28, 33, 25}}
# printing the input dictionary
print(inputDict)
輸出
執行後,上述程式將生成以下輸出:
{'Employ ID': {10, 11, 12, 13, 14, 15}, 'Employ Age': {33, 35, 40, 25, 28, 30}}
在上面的示例中,我們可以觀察到所有重複項都被移除,並且只打印了唯一元素。因此證明python集合不允許重複。
方法2:使用defaultdict()方法
在此方法中,將建立**預設集合**,並將鍵值對傳遞給它。
語法
defaultdict(set)
傳遞帶有鍵和值的字典:
dictionary[“key”] |= {‘value1’, ‘value2′, ……………,’value n’}
這裡:
dictionary - 輸入字典
key - 字典的鍵
value - 作為集合傳遞的字典的值。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字從collections模組匯入**defaultdict**。
使用**defaultdict()**方法,透過將集合作為引數傳遞給它來建立一個空的集合字典。
使用[]運算子為字典提供**鍵值對**。
列印建立的集合字典。
示例
以下程式使用defaultdict()函式在python中建立一個集合字典:
# importing defaultdict from the collections module
from collections import defaultdict
# creating an empty set of the dictionary using the
# defaultdict() method by passing set as argument to it
dictionary = defaultdict(set)
# giving the first key-value pair of a dictionary
dictionary["Employ ID"] |= {10, 11, 12, 14, 15}
# giving the second key-value pair of a dictionary
dictionary["Employ Age"] |= {25, 30, 40, 35, 28}
# printing the created dictionary of sets
print(dictionary)
輸出
執行上述程式將生成以下輸出:
defaultdict(<class 'set'>, {'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}})
方法3:使用setdefault()方法
**setdefault()**方法返回字典中鍵的值。如果沒有,則將鍵和值插入到字典中。
語法
dict.setdefault(key, default_value)
這裡:
**key** - 必須在字典中搜索的鍵。
**default_value** - 特定鍵的值
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入字典,該字典的值為不包含重複元素的集合(即集合字典)。
使用**setdefault()**函式,透過將**'Employ ID'**作為引數傳遞給它來訪問輸入字典中**'Employ ID'**的值作為集合。
使用**setdefault()**函式,透過將**'Employ Age'**作為引數傳遞給它來訪問輸入字典中**'Employ Age'**的值作為集合。
使用setdefault()方法建立一個新的鍵值對,其值為集合。
新增第三個集合後列印結果字典。
示例
以下程式使用setdefault()方法建立集合字典並訪問其元素:
# creating a dictionary containing the values as sets
# (dictionary of sets)
inputDict = {'Employ ID': {10, 11, 12, 14, 15, 11},
'Employ Age': {25, 30, 40, 35, 28, 28}}
# accessing the values of 'Employ ID' of the dictionary as a set
# using setdefault() function
print("Accessing Employ ID:", inputDict.setdefault('Employ ID'))
# accessing the values of 'Employ Age' of dictionary as a set
# using setdefault() function
print("Accessing Employ Age:", inputDict.setdefault('Employ Age'))
# set the third set of values for the dictionary using setdefault method
# Employee names
inputDict = inputDict.setdefault(
'Employ Name', {'rohit', 'virat', 'pandya', 'smith', 'warner'})
# printing the dictionary after adding 3rd set
print(inputDict)
輸出
執行上述程式將生成以下輸出:
Accessing Employ ID: {10, 11, 12, 14, 15}
Accessing Employ Age: {35, 40, 25, 28, 30}
{'pandya', 'rohit', 'smith', 'virat', 'warner'}
結論
在本文中,我們學習瞭如何使用三種不同的方法在Python中建立集合字典。這對於某些鍵移除重複資料是必要的,例如只保留唯一的學號、唯一的職位ID等。我們還學習瞭如何使用setdefault()函式檢索集合字典。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP