What are these set operations, and why do they giv

2020-03-21 10:20发布

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:

  • What is this expression called?
  • Why do I get 3 different results from 3 different Python versions?

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.

1条回答
Bombasti
2楼-- · 2020-03-21 10:54

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.

查看更多
登录 后发表回答