在Python中,求两个集合的交集可以使用 `&` 运算符或者 `intersection()` 方法。以下是使用这些方法求交集的示例代码:
使用 `&` 运算符求交集
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1 & set2
print(intersection_set) 输出:{2, 3}
使用 `intersection()` 方法求交集
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
print(intersection_set) 输出:{2, 3}
以上代码将两个集合 `set1` 和 `set2` 的交集存储在 `intersection_set` 中,并输出结果