Python Set intersection_update() メソッド

Python3 列表Python 集合


説明

intersection_update()このメソッドは、2つ以上の集合に共通する要素を取得するために使用されます。つまり、積集合を計算します。

intersection_update()メソッドは異なります。intersection()メソッドは、なぜならintersection()メソッドは新しい集合を返しますが、intersection_update()メソッドは元の集合から重複しない要素を削除します。

構文

intersection_update() メソッドの構文:

set.intersection_update(set1, set2 ... etc)

パラメータ

  • set1 -- 必須。共通する要素を検索する集合
  • set2 -- 省略可能。その他の共通する要素を検索する集合。複数指定可能で、複数の場合はカンマ , で区切ります。

戻り値

なし。

例

x 集合の中から y 集合に存在しない要素を削除します:

例 1

x = {"apple", "banana", "cherry"} # y 集合は banana と cherry を含まないため、削除されます y = {"google", "サンプル", "apple"} x.intersection_update(y) print(x)

出力結果は次のとおりです:

{'apple'}

複数の集合の積集合を計算します:

例 1

x = {"a", "b", "c"} y = {"c", "d", "e"} z = {"f", "g", "c"} x.intersection_update(y, z) print(x)

出力結果は次のとおりです:

{'c'}

Python3 列表Python 集合

その他の拡張