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

   2024-10-02 4810
核心提示:在Python中,可以使用intersection()方法或运算符来执行两个集合的交集操作。示例如下:set1 = {1, 2, 3, 4, 5}set2 = {3, 4, 5,

在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 & set2print(intersection_set)  # 输出 {3, 4, 5}

以上代码将两个集合set1和set2的交集存储在intersection_set中,并输出结果。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号