# 在 Python 中获取列表的并集

## 在 Python 中使用重复的公共元素获取两个列表的并集

``````l1 = [1,2,3,4,5]
l2 = [2,4,6.8,10]

l3 = l1 + l2

print("l1: ",l1)
print("l2: ",l2)
print("Union of l1 and l2 with element repetition: ",l3)
``````

``````l1:  [1, 2, 3, 4, 5]
l2:  [2, 4, 6.8, 10]
Union of l1 and l2 with element repetition:  [1, 2, 3, 4, 5, 2, 4, 6.8, 10]
``````

### 在 Python 中按排序顺序获取两个列表的并集

``````l1 = [11,20,1,2,3,4,5]
l2 = [2,4,6,8,10]

union_l1_l2 = l1+l2
l3 = sorted(union_l1_l2)

print("l1: ",l1)
print("l2: ",l2)
print("Sorted union of two l1 and l2 : ",l3)
``````

``````l1:  [11, 20, 1, 2, 3, 4, 5]
l2:  [2, 4, 6, 8, 10]
Sorted union of two l1 and l2 :  [1, 2, 2, 3, 4, 4, 5, 6, 8, 10, 11, 20]
``````

## 获取两个列表的并集而不重复 Python 中的公共元素

``````def union_without_repetition(list1,list2):
result = list(set(list1 + list2))
return result

l1 = [1,2,3,4,5]
l2 = [2,4,6,8,10]
l3= union_without_repetition(l1,l2)

print("l1: ",l1)
print("l2: ",l2)
print("Union of two l1 and l2 without repetition : ",l3)
``````

``````l1:  [1, 2, 3, 4, 5]
l2:  [2, 4, 6, 8, 10]
Union of two l1 and l2 without repetition :  [1, 2, 3, 4, 5, 6, 8, 10]
``````

## 在 Python 中获取两个以上列表的并集

``````def union(lst1, lst2, lst3):
final_list = list(set().union(lst1, lst2, lst3))
return final_list

l1 = [1,2,3,4,5]
l2 = [2,4,6,8,10]
l3 = [5,6,7,8,11,15,18]
print("l1: ",l1)
print("l2: ",l2)
print("l3 : ",l3)
print("Union of more than l1 l2 and l3: ",union(l1,l2,l3))
``````

``````l1:  [1, 2, 3, 4, 5]
l2:  [2, 4, 6, 8, 10]
l3 :  [5, 6, 7, 8, 11, 15, 18]
Union of more than l1 l2 and l3:  [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 15, 18]
``````

## 相关文章 - Python List

• 在 Python 中对列表列表进行排序
• Python 中从 1 到 N 的数字列表