Python集是无序项目的集合。集合中的每个元素必须是唯一的,不可变的,并且集合会删除重复的元素。集合是可变的,这意味着无涯教程可以在创建后对其进行修改,该集合的元素没有附加索引,即无法通过索引直接访问集合的任何元素。但是,可以将它们全部打印在一起,也可以通过遍历集合来获取元素列表。
可以通过用大括号{}括起逗号分隔的不可变项来创建该集合。 Python还提供了set()方法,该方法可用于通过传递的序列来创建集合。
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
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
它可以包含任何类型的元素,例如整数,浮点数,元组等。但是可变元素(列表,字典,集合)不能是集合的成员。考虑以下示例。
# 创建一个集合 set1 = {1,2,3, "Learnfk", 20.5, 14} print(type(set1)) set2 = {1,2,3,["Learnfk",4]} print(type(set2))
输出:
<class 'set'> Traceback (most recent call last) <ipython-input-5-9605bb6fbc68> in <module> 4 5 ----> 6 set2 = {1,2,3,["Learnfk",4]} 7 print(type(set2)) TypeError: unhashable type: 'list'
在上面的代码中,创建了两个集合,集合 set1 具有不可变元素,而set2具有一个可变元素作为列表。在检查set2的类型时,它引发了一个错误,这意味着set只能包含不可变元素。
创建一个空集有点不同,因为空的花括号{}也也用于创建字典。因此,Python提供了不带参数的set()方法来创建空集。
输出:
<class 'dict'> <class 'set'>
让无涯教程看看如果向集合提供重复元素会发生什么。
输出:
Return set with unique elements: {1, 2, 4, 5, 8, 9, 10}
在上面的代码中,看到 set5 由多个重复元素组成,当打印它时,从集合中删除了重复项。
Python提供了 add()方法和 update()方法,可用于将某些特定项目添加到集合中。 add()方法用于添加单个元素,而update()方法用于向集合添加多个元素。考虑以下示例。
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()方法。它接受iterable作为参数。
考虑以下示例。
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'}
Python提供了 discard()方法和 remove()方法,可用于从集合中删除项目。这些函数之间的区别在于,如果该项目不存在于集合中,则使用discard()函数,则该集合保持不变,而remove()方法将出现错误。
考虑以下示例。
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()方法删除该项目。通常,pop()方法将始终删除最后一项,但是集合是无序的,无法确定将从集合中弹出哪个元素。
考虑以下示例,使用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'}
在上面的代码中, Month 的最后一个元素是 March ,但是pop()方法删除了六月和一月,因为该集是无序,pop()方法无法确定集合的最后一个元素。
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()方法都执行相同的任务,但是dispatch()和remove()之间有一个主要区别。
如果使用set()中不存在要使用set()删除的键,则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"); #虽然SET中不可用FEB键,但不会给出错误 print("\nprinting the modified set...") print(Months) print("\nRemoving items through remove() method..."); Months.remove("Jan") #将出现错误,因为键在集合中不可用。 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'
可以执行数学运算,如并集,相交,差和对称差。 Python提供了使用运算符或方法执行这些操作的工具。无涯教程将这些操作描述如下。
使用管道(|)运算符可计算两组的并集。这两个集合的并集包含两个集合中都存在的所有项目。
考虑以下示例来计算两个集合的并集。
Days1 = {"Monday","Tuesday","Wednesday","Thursday", "Sunday"} Days2 = {"Friday","Saturday","Sunday"} print(Days1|Days2) #打印集合的并集
输出:
{'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday', 'Thursday'}
Python还提供了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'}
两组的交集可以通过&和运算符或交集()函数执行。这两个集合的交集作为两个集合中共有的元素的集合给出。
考虑以下示例。
示例1:使用&运算符
Days1 = {"Monday","Tuesday", "Wednesday", "Thursday"} Days2 = {"Monday","Tuesday","Sunday", "Friday"} print(Days1&Days2) #prints the intersection of the two sets
输出:
{'Monday', 'Tuesday'}
示例2:使用difference()方法
set1 = {"Devansh","Learnfk", "David", "Martin"} set2 = {"Steve", "Milan", "David", "Martin"} print(set1.intersection(set2)) #prints the intersection of the two sets
输出:
{'Martin', 'David'}
示例3:
输出:
{1,2,5}
intersection_update()方法从原始集中删除两个集中都不存在的项目(如果指定了多个,则为所有集中)。
intersection_update()方法与difference()方法的不同之处在于它通过删除不需要的项来修改原始集合,另一方面difference()方法返回新集合。
考虑以下示例。
a = {"Devansh", "bob", "castle"} b = {"castle", "dude", "emyway"} c = {"fuson", "gaurav", "castle"} a.intersection_update(b, c) print(a)
输出:
{'castle'}
可以使用减法(-)运算符或 difference()方法来计算两组的差。假设有两个集合A和B,且差为A-B,这表示将获得集合B中不存在的元素A的结果集合。
考虑以下示例。
示例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'}
通过^运算符或 symmetric_difference()方法计算两组的对称差。集合的对称差异,它删除了两个集合中都存在的元素。考虑以下示例:
示例-1:使用^运算符
输出:
{3, 4, 5, 6, 8, 9, 10}
示例-2:使用symmetric_difference()方法
输出:
{3, 4, 5, 6, 8, 9, 10}
Python允许对集合使用比较运算符,即<,>,<=,> =,==,通过这些运算符,可以检查集合是子集,超集还是等效于其他集。根据集合中存在的项目,返回布尔值true或false。
考虑以下示例。
Days1 = {"Monday", "Tuesday", "Wednesday", "Thursday"} Days2 = {"Monday", "Tuesday"} Days3 = {"Monday", "Tuesday", "Friday"} #Days1是Days2的超集,因此它将打印True。 print (Days1>Days2) #Day1日期不是Day2的子集以来,打印False print (Days1<Days2) print (Days2 == Days3)
输出:
True False False
frozenset是普通集的不变形式,即,frozenset的项不能更改,因此可以用作字典中的键。
创建后,frozenset合的元素无法更改。不能通过使用诸如add()或remove()之类的方法来更改或追加frozenset的内容。
Frozenset()方法用于创建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()方法内的序列,它将仅接收字典中的键,并返回一个包含字典键作为其元素的Frozenset。
考虑以下示例。
Dictionary = {"Name":"Learnfk", "Country":"USA", "ID":101} print(type(Dictionary)) Frozenset = frozenset(Dictionary); #Froozenset将包含字典的键 print(type(Frozenset)) for i in Frozenset: print(i)
输出:
<class 'dict'> <class 'frozenset'> Name Country ID
示例-1:编写程序以从集中删除给定的数字。
my_set = {1,2,3,4,5,6,12,24} n = int(input("Enter the number you want to remove")) my_set.discard(n) print("After Removing:",my_set)
输出:
Enter the number you want to remove:12 After Removing: {1, 2, 3, 4, 5, 6, 24}
示例-2:编写程序以将多个元素添加到集合中。
输出:
{1, 2, 4, 'Apple', 'Learnfk', 'CS', 'Mango', 'Grapes'}
示例-3:编写一个程序来查找两个集合之间的联合。
set1 = set(["Peter","Joseph", 65,59,96]) set2 = set(["Peter",1,2,"Joseph"]) set3 = set1.union(set2) print(set3)
输出:
{96, 65, 2, 'Joseph', 1, 'Peter', 59}
示例-4: 编写程序以查找两个集合之间的交集。
set1 = {23,44,56,67,90,45,"Learnfk"} set2 = {13,23,56,76,"Sachin"} set3 = set1.intersection(set2) print(set3)
输出:
{56, 23}
示例-5:编写程序以将元素添加到Frozenset中。
set1 = {23,44,56,67,90,45,"Learnfk"} set2 = {13,23,56,76,"Sachin"} set3 = set1.intersection(set2) print(set3)
输出:
TypeError: 'frozenset' object does not support item assignment
上面的代码提出了一个错误,因为frozenset是不可变的,创建后无法更改。
示例-6:编写程序以查找issuperset,issubset和superset。
set1 = set(["Peter","James","Camroon","Ricky","Donald"]) set2 = set(["Camroon","Washington","Peter"]) set3 = set(["Peter"]) issubset = set1 >= set2 print(issubset) issuperset = set1 <= set2 print(issuperset) issubset = set3 <= set2 print(issubset) issuperset = set2 >= set3 print(issuperset)
输出:
False False True True
Python包含以下与这些集合一起使用的方法。
SN | 方法 | 说明 |
---|---|---|
1 | add(item) | 它将一个项目添加到集合中。如果该商品已经存在于集合中,则没有任何效果。 |
2 | clear() | 它将删除集合中的所有项目。 |
3 | copy() | 它返回集合的浅表副本。 |
4 | difference_update(....) | 它通过删除指定集中还存在的所有项目来修改此集中。 |
5 | discard(item) | 它从集合中删除指定的项目。 |
6 | difference() | 它将返回一个仅包含两个集合共同元素的新集合。 (如果指定了两个以上的所有集合)。 |
7 | intersection_update(....) | 它将删除项目m在两个集合中都不存在的原始集合中(如果指定了多个集合,则指定所有集合)。 |
8 | Isdisjoint(....) | 如果两个集合的交点为空,则返回True。 |
9 | Issubset(....) | 报告另一个集合是否包含该集合。 |
10 | Issuperset(....) | 报告此集合是否包含另一个集合。 |
11 | pop() | 删除并返回作为集合最后一个元素的任意集合元素。如果集合为空,则引发KeyError。 |
12 | remove(item) | 从集合中删除一个元素;它必须是成员。如果该元素不是成员,则引发KeyError。 |
13 | symmetric_difference(....) | 从集合中删除一个元素;它必须是成员。如果元素不是成员,则引发一个KeyError。 |
14 | symmetric_difference_update(....) | 使用本身和另一个的对称差异更新一个集合。 |
15 | union(....) | 将集合的并集作为新集合返回。(即,任一集合中的所有元素。) |
16 | update() | 使用自身和其他元素的并集更新集合。 |
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)