logo

Python komplekts | atšķirība ()

Atšķirība starp abām kopām Python ir vienāda ar atšķirību starp elementu skaitu divās kopās. Funkcija atšķirība () atgriež kopu, kas ir atšķirība starp divām kopām. Mēģināsim noskaidrot, kāda būs atšķirība starp divām kopām A un B. Tad (kopa A – kopa B) būs elementi, kas atrodas kopā A, bet ne B, un (kopa B – kopa A) būs elementi, kas atrodas kopā komplektā B, bet ne A komplektā.

Piemērs:



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>

Apskatīsim šādas atšķirību kopas funkcijas Venna diagrammu. Sintakse:

 set_A.difference(set_B) for (A - B) set_B.difference(set_A) for (B - A)>

Šajā programmā mēs mēģināsim noskaidrot atšķirību starp divām kopām set_A un set_B, abos veidos:

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))>

iekapsulēšana java

>

>

Izvade:

{10, 20} {100, 60}>

Mēs varam izmantot arī operatoru, lai atrastu atšķirību starp divām kopām.

Python3




vicky kaushal vecums
# 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)>

>

>

Izvade:

{10, 20} {100, 60}>

Ja mums ir vienādas kopas, tas atgriezīs nulles kopu.

Python3


anakonda vs pitona čūska



# 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)>

>

>

Izvade:

set()>