Python symmetric_difference () -menetelmä palauttaa kahden joukon symmetrisen eron.
Kahden joukon A ja B symmetrinen ero on joukko elementtejä, jotka ovat joko A: ssa tai B: ssä, mutta eivät niiden leikkauspisteessä.
![](https://cdn.wiki-base.com/7100826/python_set_symmetric_difference.png.webp)
Syntaksi symmetric_difference()
on:
A.symmetrinen_ero (B)
Esimerkki 1: Symmetrisen_eron () käyttö
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))
Tuotos
('b', 'a', 'e') ('b', 'e', 'a') ('b', 'd', 'c', 'a') ('d', 'e ',' c ')
Symmetrinen ero -operaattorilla
Pythonissa löydämme myös symmetrisen eron ^
operaattorin avulla.
A = ('a', 'b', 'c', 'd') B = ('c', 'd', 'e' ) print(A B) print(B A) print(A A) print(B B)
Tuotos
('e', 'a', 'b') ('e', 'a', 'b') sarja () sarja ()