union() 方法返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。
union() 方法语法:
set.union(set1, set2...)
参数:
- set1 – 必需,合并的目标集合
- set2 – 可选,其他要合并的集合,可以多个,多个使用逗号 , 隔开。
例01
x = {
"apple", "banana", "cherry"}
y = {
"google", "runoob", "apple"}
z = x.union(y)
print(z)
{
'cherry', 'runoob', 'google', 'banana', 'apple'}
例02
# *拆解列表后,转为为多个独立的元素作为参数给union,union函数进行去重合并
unique_nodes_list = list(set.union(*samp_neighs))