I had seen this test question on Pluralsight:
Given these sets:
x = {'a', 'b', 'c', 'd'}
y = {'c', 'e', 'f'}
z = {'a', 'g', 'h', 'i'}
What is the value of x | y ^ z
?
The expected answer is:
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
Combines the sets (automatically discarding duplicates), and orders them from lowest to greatest.
My questions are:
Result on Python 3.7.5 on Ubuntu 18.04:
{'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'}
Result on Python 2.17.17rc1 on Ubuntu 18.04:
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h'])
Result on Python 3.7.2 on Windows 10:
{'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}
Here is a repl of the same code I'm using for this: https://repl.it/repls/RudeMoralWorkplace
I'd like to understand what happens behind the scenes with these expressions so I can debunk why I get different results.
There are four main set operations which include set union, set intersection, set complement, and set difference.
2 Set Operations. The union of two sets is a set containing all elements that are in A or in B (possibly both). For example, {1,2}∪{2,3}={1,2,3}. Thus, we can write x∈(A∪B) if and only if (x∈A) or (x∈B).
The set operations you have mentioned are:
^
- symmetric difference (XOR):
Return a new set with elements in either the set or other but not both.
Example: {'1', '2', '3'} ^ {'2', '3', '4'} = {'1', '4'}
|
- union (OR):
Return a new set with elements from the set and all others.
Example: {'1', '2', '3'} | {'2', '3', '4'} = {'1', '2', '3', '4'}
There are also other set operations in python:
&
- intersection (AND):
Return a new set with elements common to the set and all others.
Example: {'1', '2', '3'} & {'2', '3', '4'} = {'2', '3'}
-
- difference:
Return a new set with elements in the set that are not in the others.
Example: {'1', '2', '3'} - {'2', '3', '4'} = {'1'}
The order of precedence for these operations is -, &, ^, |
, so in your example, we first apply ^
:
>>> y^z
{'a', 'c', 'e', 'f', 'g', 'h', 'i'}
And then |
:
>>> x|{'a', 'c', 'e', 'f', 'g', 'h', 'i'}
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
The different outputs you describe are actually the same set, as sets are not ordered.
>>> {'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'} == {'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}
True
Any order shown in the string representation of a set is an implementation detail and should not be relied upon as it will vary unpredictably, as you have found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With