溫馨提示×

Python列表與集合怎么并集

小億
120
2024-05-06 15:21:58
欄目: 編程語言

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 | set1
print(result)

以上兩種方法都會得到結果為[1, 2, 3, 4, 5, 6]的并集。

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