如何在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