Anonymous user
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 <
<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'}
>>> </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.
<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
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}}==
|