📅  最后修改于: 2020-09-21 02:27:45             🧑  作者: Mango
Python提供了一个名为set的数据类型,其元素必须是唯一的。它可用于执行不同的设置操作,例如并集,交集,差和对称差。
# Program to perform different set operations like in mathematics
# define three sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("Union of E and N is",E | N)
# set intersection
print("Intersection of E and N is",E & N)
# set difference
print("Difference of E and N is",E - N)
# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)
输出
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}
在此程序中,我们采用两个不同的集合并对它们执行不同的集合操作。同样可以通过使用set方法来完成。