这些集合操作是什么,为什么它们会给出不同的结果? [英] What are these set operations, and why do they give different results?

查看:68
本文介绍了这些集合操作是什么,为什么它们会给出不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 个不同的结果?
<小时>

Python 3.7.5 在 Ubuntu 18.04 上的结果:

{'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

我想了解这些表达式在幕后发生了什么,以便我可以揭穿为什么我得到不同的结果.

解决方案

你提到的集合操作是:

^ - 对称差异(异或):

<块引用>

返回一个新集合,其中包含该集合中的元素或其他元素,但不能同时包含两者.

示例: {'1', '2', '3'} ^ {'2', '3', '4'} = {'1', '4'}

| - union(或):

<块引用>

返回一个包含集合中的元素和所有其他元素的新集合.

示例: {'1', '2', '3'} |{'2', '3', '4'} = {'1', '2', '3', '4'}

python中还有其他set操作:

& - 交集(AND):

<块引用>

返回一个新集合,其中包含与集合和所有其他元素共有的元素.

示例: {'1', '2', '3'} &{'2', '3', '4'} = {'2', '3'}

--差异:

<块引用>

返回一个新集合,该集合中的元素不在其他集合中.

示例: {'1', '2', '3'} - {'2', '3', '4'} = {'1'}

这些操作的优先顺序是-, &, ^, |,所以在你的例子中,我们首先应用^:

<预><代码>>>>y^z{'a', 'c', 'e', 'f', 'g', 'h', 'i'}

然后|:

<预><代码>>>>x|{'a', 'c', 'e', 'f', 'g', 'h', 'i'}{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}

你描述的不同输出实际上是同一个集合,因为集合没有排序.

<预><代码>>>>{'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'} == {'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}真的

集合的字符串表示中显示的任何顺序都是实现细节,不应依赖,因为正如您所发现的,它会发生不可预测的变化.

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.

解决方案

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.

这篇关于这些集合操作是什么,为什么它们会给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆