在Python中,可以使用集合的intersection()方法来实现两个集合的交集。示例如下:
set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}intersection_set = set1.intersection(set2)print(intersection_set)输出结果为:
{4, 5}另外,也可以使用&操作符来实现两个集合的交集,示例如下:
intersection_set = set1 & set2print(intersection_set)输出结果仍然为:
{4, 5} 

