2025年10月18日 14時24分

【Python】集合の対称差演算子

最終更新:2025年10月18日 14時24分

集合A ^ 集合Bで、集合Aと集合Bの差の集合ができる。

ソースコード

symmetric_difference.py · ki-hi-ro/python@2f5ca39

set_a = {1, 2, 3, 4, 5}
set_b = {2, 3}
symmetric_diff = set_a ^ set_b
print("Symmetric difference between set_a and set_b:", symmetric_diff)

symmetric_diffに集合の対称差が入る。

実行結果

Symmetric difference between set_a and set_b: {1, 4, 5}

set_aとset_bの対称差は、{1, 4, 5}

set_a = {1, 2, 3, 4, 5}
set_b = {2, 3}

カテゴリー:技術・開発

タグ: