set.intersection() 積集合
最終更新:2026-09-22
set.intersection() 積集合
set.intersection(*others) は set とすべての other に共通する要素を返す。
| カテゴリ | 集合メソッド |
|---|---|
| 種類 | 組み込み型メソッド |
| Python バージョン | all |
📝 構文
set.intersection(*others)
⚙️ 引数
| others 任意 | Zero or more iterables; can be omitted. |
|---|
戻り値:A new set with elements common to set and all others.
元のオブジェクトを変更するか:いいえ(新しいオブジェクトを返す)
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
{2, 3}
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qintersection() と & 演算子の違いは何ですか?
A& 演算子は両側とも集合でなければなりません。intersection() の引数は任意のイテラブルにできます。
Q新しい集合を返しますか?
Aはい。元の集合は変わりません。積をその場に保つには intersection_update() を使います。
Q共通要素がないときは何を返しますか?
Aエラーを発生させずに空集合 set() を返します。