Python における 2 つのセットの差は、2 つのセットの要素数の差に等しくなります。関数Difference()は、2つのセットの差であるセットを返します。 2 つのセット A と B の違いを調べてみましょう。すると、(セット A – セット B) はセット A には存在するが B には存在しない要素となり、(セット B – セット A) は存在する要素になります。セットBにはありますが、セットAにはありません。
例:
set A = {10, 20, 30, 40, 80} set B = {100, 30, 80, 40, 60} set A - set B = {10, 20} set B - set A = {100, 60} Explanation: A - B is equal to the elements present in A but not in B B - A is equal to the elements present in B but not in A> 次の差集合関数のベン図を見てみましょう。
構文:
set_A.difference(set_B) for (A - B) set_B.difference(set_A) for (B - A)>
このプログラムでは、2 つのセット set_A と set_B の違いを両方の方法で見つけようとします。
Python3
# Python code to get the difference between two sets> # using difference() between set A and set B> # Driver Code> A>=> {>10>,>20>,>30>,>40>,>80>}> B>=> {>100>,>30>,>80>,>40>,>60>}> print> (A.difference(B))> print> (B.difference(A))> |
チランジーヴィ俳優
>
>出力:
世界で一番の笑顔
{10, 20} {100, 60}> – 演算子を使用して 2 つのセット間の違いを見つけることもできます。
Python3
# Python code to get the difference between two sets> # using difference() between set A and set B> # Driver Code> A>=> {>10>,>20>,>30>,>40>,>80>}> B>=> {>100>,>30>,>80>,>40>,>60>}> print> (A>-> B)> print> (B>-> A)> |
>
>出力:
{10, 20} {100, 60}> 等しいセットがある場合は、null セットが返されます。
Python3
棚の犬
# Python code to get the difference between two sets> # using difference() between set A and set B> # Driver Code> A>=> {>10>,>20>,>30>,>40>,>80>}> B>=> {>10>,>20>,>30>,>40>,>80>,>100>}> print> (A>-> B)> |
>
>出力:
set()>