数据类型-集合set
python中的集合可以定义为括在花括号中的各种项目的无序集合。集合的元素不能重复。python集的元素必须是不可变的。
与python中的其他集合不同,没有索引附加到集合的元素,即,我们不能通过索引直接访问集合的任何元素。但是,我们可以将它们一起打印出来,或者我们可以通过循环遍历集合来获取元素列表。
创建一个集合
可以通过用逗号括起逗号分隔的项来创建该集。Python还提供了set方法,可用于通过传递的序列创建集合。
示例1:使用花括号
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} print(Days) print(type(Days)) print("looping through the set elements ... ") for i in Days: print(i)
输出:
{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'} <class 'set'> looping through the set elements ... Friday Tuesday Monday Saturday Thursday Sunday Wednesday
示例2:使用set()方法
Days = set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]) print(Days) print(type(Days)) print("looping through the set elements ... ") for i in Days: print(i)
输出:
{'Friday', 'Wednesday', 'Thursday', 'Saturday', 'Monday', 'Tuesday', 'Sunday'} <class 'set'> looping through the set elements ... Friday Wednesday Thursday Saturday Monday Tuesday Sunday
Python Set操作
在前面的示例中,我们讨论了如何在python中创建集合。但是,我们可以对python集执行各种数学运算,如union,intersection,difference等。
将项目添加到集合中 add()
Python提供了add()方法,可用于将一些特定项添加到集合中。请考虑以下示例。
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nAdding other months to the set..."); Months.add("July"); Months.add("August"); print("\nPrinting the modified set..."); print(Months) print("\nlooping through the set elements ... ") for i in Months: print(i)
输出:
printing the original set ... {'February', 'May', 'April', 'March', 'June', 'January'} Adding other months to the set... Printing the modified set... {'February', 'July', 'May', 'April', 'March', 'August', 'June', 'January'} looping through the set elements ... February July May April March August June January
要在集合中添加多个项目,Python提供了update()方法。
请考虑以下示例。
例
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nupdating the original set ... ") Months.update(["July","August","September","October"]); print("\nprinting the modified set ... ") print(Months);
输出:
printing the original set ... {'January', 'February', 'April', 'May', 'June', 'March'} updating the original set ... printing the modified set ... {'January', 'February', 'April', 'August', 'October', 'May', 'June', 'July', 'September', 'March'}
从集中删除项目 discard()
Python提供了discard()方法,可用于从集合中删除项目。
请考虑以下示例。
例
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nRemoving some months from the set..."); Months.discard("January"); Months.discard("May"); print("\nPrinting the modified set..."); print(Months) print("\nlooping through the set elements ... ") for i in Months: print(i)
输出:
printing the original set ... {'February', 'January', 'March', 'April', 'June', 'May'} Removing some months from the set... Printing the modified set... {'February', 'March', 'April', 'June'} looping through the set elements ... February March April June
Python还提供了remove()方法来从集合中删除项目。请考虑以下示例以使用remove()方法删除项目。
例
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nRemoving some months from the set..."); Months.remove("January"); Months.remove("May"); print("\nPrinting the modified set..."); print(Months)
输出:
printing the original set ... {'February', 'June', 'April', 'May', 'January', 'March'} Removing some months from the set... Printing the modified set... {'February', 'June', 'April', 'March'}
我们也可以使用pop()方法删除该项。但是,此方法将始终删除最后一项。
请考虑以下示例从集合中删除最后一项。
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nRemoving some months from the set..."); Months.pop(); Months.pop(); print("\nPrinting the modified set..."); print(Months)
输出:
printing the original set ... {'June', 'January', 'May', 'April', 'February', 'March'} Removing some months from the set... Printing the modified set... {'May', 'April', 'February', 'March'}
Python提供了clear()方法来从集合中删除所有项目。
请考虑以下示例。
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nRemoving all the items from the set..."); Months.clear() print("\nPrinting the modified set...") print(Months)
输出:
printing the original set ... {'January', 'May', 'June', 'April', 'March', 'February'} Removing all the items from the set... Printing the modified set... set()
discard()和remove()之间的区别
尽管discard()和remove()方法都执行相同的任务,但discard()和remove()之间存在一个主要区别。
如果在集合中不存在使用discard()从集合中删除的密钥,则python不会给出错误。该程序保持其控制流程。
另一方面,如果集合中不存在要使用remove()从集合中删除的项目,则python将给出错误。
请考虑以下示例。
例
Months = set(["January","February", "March", "April", "May", "June"]) print("\nprinting the original set ... ") print(Months) print("\nRemoving items through discard() method..."); Months.discard("Feb"); #will not give an error although the key feb is not available in the set print("\nprinting the modified set...") print(Months) print("\nRemoving items through remove() method..."); Months.remove("Jan") #will give an error as the key jan is not available in the set. print("\nPrinting the modified set...") print(Months)
输出:
printing the original set ... {'March', 'January', 'April', 'June', 'February', 'May'} Removing items through discard() method... printing the modified set... {'March', 'January', 'April', 'June', 'February', 'May'} Removing items through remove() method... Traceback (most recent call last): File "set.py", line 9, in Months.remove("Jan") KeyError: 'Jan'
两个集合并集 union
两组的并集是使用or(|)运算符计算的。两个集合的并集包含两个集合中存在的所有项目。
请考虑以下示例来计算两个集合的并集。
例1:使用union | 操作者
Days1 = {"Monday","Tuesday","Wednesday","Thursday"} Days2 = {"Friday","Saturday","Sunday"} print(Days1|Days2) #printing the union of the sets
输出:
{'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday', 'Thursday'}
Python还提供了union()方法,该方法也可用于计算两个集合的并集。请考虑以下示例。
示例2:使用union()方法
Days1 = {"Monday","Tuesday","Wednesday","Thursday"} Days2 = {"Friday","Saturday","Sunday"} print(Days1.union(Days2)) #printing the union of the sets
输出:
{'Friday', 'Monday', 'Tuesday', 'Thursday', 'Wednesday', 'Sunday', 'Saturday'}
两个集合交集 &
&(intersection)运算符用于计算python中两个集合的交集。两组的交集作为两组中共同的元素集给出。
请考虑以下示例。
示例1:使用&运算符
set1 = {"Ayush","John", "David", "Martin"} set2 = {"Steve","Milan","David", "Martin"} print(set1&set2) #prints the intersection of the two sets
输出:
{'Martin', 'David'}
示例2:使用intersection()方法
set1 = {"Ayush","John", "David", "Martin"} set2 = {"Steave","Milan","David", "Martin"} print(set1.intersection(set2)) #prints the intersection of the two sets
输出:
{'Martin', 'David'}
intersection_update()方法
intersection_update()方法从原始集中删除两个集合中不存在的项目(如果指定了多个集合,则为所有集合)。
Intersection_update()方法与intersection()方法不同,因为它通过删除不需要的项来修改原始集,另一方面,intersection()方法返回一个新集。
请考虑以下示例。
a = {"ayush", "bob", "castle"} b = {"castle", "dude", "emyway"} c = {"fuson", "gaurav", "castle"} a.intersection_update(b, c) print(a)
Output:
{'castle'}
两个集合的差集 -
可以使用减法( - )运算符计算两组的差异。将通过移除集合2中存在的集合1中的所有元素来获得所得到的集合。
请考虑以下示例。
示例1:使用减法( - )运算符
Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"} Days2 = {"Monday", "Tuesday", "Sunday"} print(Days1-Days2) #{"Wednesday", "Thursday" will be printed}
输出:
{'Thursday', 'Wednesday'}
示例2:使用difference()方法
Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"} Days2 = {"Monday", "Tuesday", "Sunday"} print(Days1.difference(Days2)) # prints the difference of the two sets Days1 and Days2
输出:
{'Thursday', 'Wednesday'}
两个集合的比较
Python允许我们使用比较运算符,即<,>,<=,> =,==使用集合,我们可以使用它来检查集合是否是子集,超集或等同于其他集合。返回布尔值true或false,具体取决于集合中存在的项目。
请考虑以下示例。
Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"} Days2 = {"Monday", "Tuesday"} Days3 = {"Monday", "Tuesday", "Friday"} #Days1 is the superset of Days2 hence it will print true. print (Days1>Days2) #prints false since Days1 is not the subset of Days2 print (Days1<Days2) #prints false since Days2 and Days3 are not equivalent print (Days2 == Days3)
输出:
True False False
FrozenSets
冻结集是普通集的不可变形式,即冻结集的项不能改变,因此它可以用作字典中的密钥。
创建后无法更改冻结集的元素。我们不能使用add()或remove()等方法更改或附加冻结集的内容。
frozenset()方法用于创建frozenset对象。可迭代序列被传递到此方法中,该方法被转换为冻结集作为方法的返回类型。
请考虑以下示例来创建冻结集。
Frozenset = frozenset([1,2,3,4,5]) print(type(Frozenset)) print("\nprinting the content of frozen set...") for i in Frozenset: print(i); Frozenset.add(6) #gives an error since we cannot change the content of Frozenset after creation
输出:
<class 'frozenset'> printing the content of frozen set... 1 2 3 4 5 Traceback (most recent call last): File "set.py", line 6, in <module> Frozenset.add(6) #gives an error since we can change the content of Frozenset after creation AttributeError: 'frozenset' object has no attribute 'add'
Frozenset字典
如果我们将字典作为firset()方法内的序列传递,它将只接受字典中的键并返回包含字典键作为其元素的冻结集。
请考虑以下示例。
Dictionary = {"Name":"John", "Country":"USA", "ID":101} print(type(Dictionary)) Frozenset = frozenset(Dictionary); #Frozenset will contain the keys of the dictionary print(type(Frozenset)) for i in Frozenset: print(i)
输出:
<class 'dict'> <class 'frozenset'> Name Country ID
Python内置集方法
Python包含以下集合方法。
SN | 方法 | 描述 |
1 | add(item) | 它会在集合中添加一个项目。如果项目已存在于集合中,则无效。 |
2 | clear() | 它会删除集合中的所有项目。 |
3 | copy() | 它返回该集的浅表副本。 |
4 | difference_update(....) | 它通过删除指定集中也存在的所有项来修改此集。 |
5 | discard(item) | 它从集合中删除指定的项目。 |
6 | intersection() | 它返回一个新集合,该集合仅包含两个集合的公共元素。(如果指定了两个以上,则为所有集合)。 |
7 | intersection_update(....) | 它从原始集中删除两个集合中不存在的项目(如果指定了多个集合,则为所有集合)。 |
8 | Isdisjoint(....) | 如果两个集合具有空交集,则返回True。 |
9 | Issubset(....) | 报告另一个集合是否包含此集合。 |
10 | Issuperset(....) | 报告此集是否包含另一个集。 |
11 | pop() | 删除并返回作为集合的最后一个元素的任意set元素。如果集合为空,则引发KeyError。 |
12 | remove(item) | 从集合中删除元素; 它必须是会员。如果元素不是成员,则引发KeyError。 |
13 | symmetric_difference(....) | 从集合中删除元素; 它必须是会员。如果元素不是成员,则引发KeyError。 |
14 | symmetric_difference_update(....) | 更新具有自身和另一个的对称差异的集合。 |
15 | union(....) | 将集合的并集作为新集合返回。 |
16 | update() | 使用自身和其他人的联合更新集合。 |