Python 如何创建字典的集合
在本文中,我们将学习如何在Python中创建一个字典的集合。
使用的方法
以下是用于完成此任务的各种方法:
- 使用朴素方法
-
使用defaultdict()方法
-
使用setdefault()方法
方法1:使用朴素方法
在这种方法中,我们通过将集合作为值传递给键来创建字典的集合。
语法
{ ‘Key’: Set 1, ‘Key’:Set 2,…………..,’Key’: Set n}
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量用于存储具有不重复元素的字典(即字典的集合)。
-
打印输入的字典集合。
示例
以下程序使用Naive方法在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中的集合 set 不允许重复元素,即删除所有重复的元素。
示例
下面的示例显示了Python集合不允许重复项。
以下程序使用Naive方法在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'}
结论
在本文中,我们学习了如何使用3种不同的方法在Python中创建一个字典集合。这对于某些键值去除重复数据是必要的,例如只保留独特的学号、独特的工作 ID 等。我们还学习了如何使用 setdefault() 函数来检索字典集合。