Jump to content

Symmetric difference: Difference between revisions

→‎{{header|Python}}: Set methods, some reformatting
(→‎{{header|jq}}: give example to show lists with repetitions are allowed)
(→‎{{header|Python}}: Set methods, some reformatting)
Line 1,799:
 
=={{header|Python}}==
Python's <ttcode>set</ttcode> type supports difference as well as symmetric difference operators.
 
;'''Python 3.Xx''' and '''Python 2.7''' have syntax for set literals:
 
<lang python>>>> setA = {"John", "Bob", "Mary", "Serena"}
>>> setB = {"Jim", "Mary", "John", "Bob"}
Line 1,809 ⟶ 1,811:
>>> setB - setA # elements in B that are not in A
{'Jim'}
>>> setBsetA -| setAsetB # elements in BA thator areB not in A(union)
>>> </lang>
{'John', 'Bob', 'Jim', 'Serena', 'Mary'}
;Earlier versions of python :
>>> setA & setB # elements in both A and B (intersection)
{'Bob', 'John', 'Mary'}</lang>
 
Note that the order of set elements is undefined.
 
;Earlier versions of python Python:
 
<lang python>>>> setA = set(["John", "Bob", "Mary", "Serena"])
>>> setB = set(["Jim", "Mary", "John", "Bob"])
Line 1,817 ⟶ 1,826:
>>> setA - setB # elements in A that are not in B
set(['Serena'])
>>> # and so on...</lang>
>>> setB - setA # elements in B that are not in A
 
set(['Jim'])</lang>
There is also a method call interface for these operations:
 
<lang python>>>> setA.symmetric_difference(setB)
{'Jim', 'Serena'}
>>> setA.difference(setB)
{'Serena'}
>>> setB.difference(setA)
{'Jim'}
>>> setA.union(setB)
{'Jim', 'Mary', 'Serena', 'John', 'Bob'}
>>> setA.intersection(setB)
{'Mary', 'John', 'Bob'}</lang>
 
=={{header|R}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.