Python列表与集合怎么并集

   2024-10-20 3820
核心提示:Python中可以使用union()方法或者|运算符来实现列表与集合的并集操作。使用union()方法示例如下:list1 = [1, 2, 3, 4

Python中可以使用union()方法或者|运算符来实现列表与集合的并集操作。

使用union()方法示例如下:

list1 = [1, 2, 3, 4]set1 = {3, 4, 5, 6}result = list1.union(set1)print(result)

使用|运算符示例如下:

list1 = [1, 2, 3, 4]set1 = {3, 4, 5, 6}result = list1 | set1print(result)

以上两种方法都会得到结果为[1, 2, 3, 4, 5, 6]的并集。

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

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