Python Setintersection ()メソッド
例
x
setとsetの両方に存在するアイテムを含むセットを返します
y
。
x =
{"apple", "banana", "cherry"}
y = {"google",
"microsoft", "apple"}
z = x.intersection(y)
print(z)
定義と使用法
このintersection()
メソッドは、2つ以上のセット間の類似性を含むセットを返します。
意味:返されるセットには、両方のセットに存在するアイテムのみが含まれます。比較が3つ以上のセットで行われる場合は、すべてのセットに含まれます。
構文
set.intersection(set1, set2 ... etc)
パラメータ値
Parameter | Description |
---|---|
set1 | Required. The set to search for equal items in |
set2 | Optional. The other set to search for equal items in. You can compare as many sets you like. Separate the sets with a comma |
その他の例
例
3つのセットを比較し、3つのセットすべてに存在するアイテムを含むセットを返します。
x =
{"a", "b", "c"}
y = {"c",
"d", "e"}
z = {"f",
"g", "c"}
result = x.intersection(y, z)
print(result)