溫馨提示×

set() python如何進行交集操作

小樊
123
2024-07-29 14:22:10
欄目: 編程語言

在Python中,可以使用intersection()方法或&運算符來執行兩個集合的交集操作。示例如下:

set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}

# 使用intersection()方法
intersection_set = set1.intersection(set2)
print(intersection_set)  # 輸出 {3, 4, 5}

# 使用&運算符
intersection_set = set1 & set2
print(intersection_set)  # 輸出 {3, 4, 5}

以上代碼將兩個集合set1和set2的交集存儲在intersection_set中,并輸出結果。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女