Python iestatiet symmetric_difference ()

Metode Python symmetric_difference () atgriež divu kopu simetrisko starpību.

Divu kopu A un B simetriskā atšķirība ir elementu kopa, kas atrodas vai nu A, vai B, bet ne to krustpunktā.

Divu kopu simetriskā atšķirība

Sintakse symmetric_difference()ir:

 A. simetriskā_diference (B) 

1. piemērs: symmetric_difference () darbība

 A = ('a', 'b', 'c', 'd') B = ('c', 'd', 'e' ) C = () print(A.symmetric_difference(B)) print(B.symmetric_difference(A)) print(A.symmetric_difference(C)) print(B.symmetric_difference(C))

Rezultāts

 ('b', 'a', 'e') ('b', 'e', ​​'a') ('b', 'd', 'c', 'a') ('d', 'e "," c ")

Simetriska atšķirība, izmantojot operatoru ^

Python, izmantojot ^operatoru , varam atrast arī simetrisko atšķirību .

 A = ('a', 'b', 'c', 'd') B = ('c', 'd', 'e' ) print(A B) print(B A) print(A A) print(B B)

Rezultāts

 ('e', 'a', 'b') ('e', 'a', 'b') set () set ()

Interesanti raksti...