Python|两个或多个列表的并集
列表的并集意味着,我们必须从列表 A 和列表 B 中取出所有元素(可以有两个以上的列表)并将它们放入一个新列表中。有多种顺序可以组合列表。例如,我们可以保持重复和顺序或删除最终列表中的重复元素等。
例子:
Maintained repetition only
Input :
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
Output :
[23, 15, 2, 14, 14, 16, 20, 52, 2, 48,
15, 12, 26, 32, 47, 54]
Maintained repetition and order
Input :
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
Output :
[2, 2, 12, 14, 14, 15, 15, 16, 20, 23,
26, 32, 47, 48, 52, 54]
Without repetition
Input :
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
Output :
[32, 2, 12, 14, 15, 16, 48, 47, 20, 52, 54, 23, 26]
Union of three lists
Input :
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
lst3 = [4, 78, 5, 6, 9, 25, 64, 32, 59]
Output :
[32, 64, 2, 4, 5, 6, 9, 12, 14, 15, 16,
48, 47, 78, 20, 52, 54, 23, 25, 26, 59]
保持重复
我们可以简单地使用加号“+”运算符将两个列表合二为一。这将反映重复。
# Python program to illustrate union
# Maintained repetition
def Union(lst1, lst2):
final_list = lst1 + lst2
return final_list
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
print(Union(lst1, lst2))
输出:
[23, 15, 2, 14, 14, 16, 20, 52, 2, 48, 15,
12, 26, 32, 47, 54]
保持重复和秩序
为了保持新列表中出现的顺序,我们需要使用sorted()函数,将两个列表的相加(加上操作,如上一个问题)作为参数传递。
# Python program to illustrate union
# Maintained repetition and order
def Union(lst1, lst2):
final_list = sorted(lst1 + lst2)
return final_list
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
print(Union(lst1, lst2))
输出:
[2, 2, 12, 14, 14, 15, 15, 16, 20, 23, 26, 32, 47, 48, 52, 54]
不重复
为了摆脱初始列表中的所有重复元素,我们分别在两个列表上使用set()函数。然后我们使用“+”运算符添加它们并作为新列表传递。
# Python program to illustrate union
# Without repetition
def Union(lst1, lst2):
final_list = list(set(lst1) | set(lst2))
return final_list
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
print(Union(lst1, lst2))
输出:
[32, 2, 12, 14, 15, 16, 48, 47, 20,
52, 54, 23, 26]
两个以上的列表
我们还可以合并两个以上的列表。这可以通过同时使用set() 和 union()函数来有效地完成,如下例所示。这也照顾重复并防止它们。
# Python program to illustrate union
# Union of three lists
def Union(lst1, lst2, lst3):
final_list = list(set().union(lst1, lst2, lst3))
return final_list
# Driver Code
lst1 = [23, 15, 2, 14, 14, 16, 20 ,52]
lst2 = [2, 48, 15, 12, 26, 32, 47, 54]
lst3 = [4, 78, 5, 6, 9, 25, 64, 32, 59]
print(Union(lst1, lst2, lst3))
输出:
[32, 64, 2, 4, 5, 6, 9, 12, 14, 15, 16,
48, 47, 78, 20, 52, 54, 23, 25, 26, 59]