Python Set union()メソッド
例
両方のセットのすべてのアイテムを含むセットを返します。重複は除外されます。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.union(y)
print(z)
定義と使用法
このunion()
メソッドは、元のセットのすべてのアイテムと、指定されたセットのすべてのアイテムを含むセットを返します。
必要な数のセットをコンマで区切って指定できます。
セットである必要はなく、任意の反復可能なオブジェクトにすることができます。
アイテムが複数のセットに存在する場合、結果にはこのアイテムの外観が1つだけ含まれます。
構文
set.union(set1, set2...)
パラメータ値
Parameter | Description |
---|---|
set1 | Required. The iterable to unify with |
set2 | Optional. The other iterable to unify with. You can compare as many iterables as you like. Separate each iterable with a comma |
その他の例
例
2セット以上を統合します。
x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z)
print(result)