意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Python列表与集合怎么并集

来源:恒创科技 编辑:恒创科技编辑部
2024-05-07 14:01:35

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

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

list1 = [1, 2, 3, 4]
set1 = {3, 4, 5, 6}

result = list1.union(set1)
print(result)

使用|运算符示例如下:


Python列表与集合怎么并集

list1 = [1, 2, 3, 4]
set1 = {3, 4, 5, 6}

result = list1 | set1
print(result)

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

上一篇: 数组操作在Python游戏开发中怎么应用 下一篇: Linux数据库系统的高可用与负载均衡怎么配置