【发布时间】:2020-05-22 19:06:07
【问题描述】:
我在 Pluralsight 上看到过这个测试题:
鉴于这些集合:
x = {'a', 'b', 'c', 'd'}
y = {'c', 'e', 'f'}
z = {'a', 'g', 'h', 'i'}
x | y ^ z 的值是多少?
预期的答案是:
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
组合集合(自动丢弃重复项),并将它们从最低到最高排序。
我的问题是:
- 这个表达式叫什么?
- 为什么我会从 3 个不同的 Python 版本中得到 3 个不同的结果?
Ubuntu 18.04 上 Python 3.7.5 的结果:
{'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'}
Ubuntu 18.04 上 Python 2.17.17rc1 的结果:
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h'])
Windows 10 上 Python 3.7.2 的结果:
{'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}
这是我为此使用的相同代码的副本: https://repl.it/repls/RudeMoralWorkplace
我想了解这些表达式在幕后发生了什么,这样我就可以揭穿为什么会得到不同的结果。
【问题讨论】:
标签: python python-3.x python-2.7 set set-operations