set1 = set(map(int, input("Set 1: ").split()))
set2 = set(map(int, input("Set 2: ").split()))
intersection = set1.intersection(set2)
print("Intersection of the two sets:", intersection)
在该程序中,我们首先使用input()
函数读取用户输入的两个集合。我们使用map()
函数将输入拆分为整数列表,然后使用set()
函数创建两个集合。
使用set.intersection()
方法获取两个集合的交集。
最后,我们打印出交集并以以下方式呈现给用户:
Intersection of the two sets: {1, 3, 5, 7, 9}