English 中文(简体)
为什么“+”不为“灰色”所理解?
原标题:Why is + not understood by Python sets?
  • 时间:2011-10-07 20:02:23
  •  标签:
  • python
  • set

我想知道,为什么这样做是有效的:

set(range(10)) - set(range(5))

但这并不是有效的:

set(range(10)) + set(range(5))

这是因为“+”可能意味着交叉和结合?

最佳回答

<代码>+的操作器没有安装。

You can use | for set union and & for set intersection.

套件确实执行<条码>-,作为定型改动。 您也可使用<代码>>^,用于计量差异(即,只有一组物体出现但两套均不出现的新物体才能返回)。

问题回答

灰 Python选择使用<条码>>而不是<条码>+,因为固定的结合是一个与细菌脱节密切相关的概念;比方病媒(在python只是int/long)将这种操作定义为一系列的生物价值,并称其为“比喻或”。 事实上,这一行动与固定的结合非常相似,即双职工有时也被称为“Bit套”,其中确定的要素是自然数字。

由于<代码>int 已确定的类似操作者为>>>,&>>>,新手的<代码>>><>>>>>/代码”类型使用同一接口是自然的。

在固定理论中,+符号通常指两组中的disjoint Union。 如果是A和B,则其联合工会的定义是:

A + B = {(a, 1) | a in A} U {(b, 2) | b in B}

即,为了建立联合联盟,我们标注A的所有要素和B的所有组成部分(例如,我使用了第1和第2号数字,但任何两个不同的“顺序”都会做工作),然后将这两套结果合并起来。 在上述例子中,我利用U来建立工会,使之与通常的数学名称更加相似;在我下面,它使用沙尔文(即:工会)和“安乐”等。

如果A和B互不关联,A+B与A>B有1至1 correspondence。 如果没有,则A &的所有共同要素x;B在A+B中是两次:一次(x,1),一次(x,2)。

So, since the + symbol has a quite well-established meaning as a set operation, I find it very consistent that Python does not use this symbol for set union or intersection. Probably Python designer(s) had this in mind when they chose set operators.

他们可以使用<条码>+<>/条码>进行工会活动,但仍需有交叉的象征。 <代码>>> 工会编码是同<代码>和p>的等值编码;对交界进行更好的选择。

Because | means union and & means intersection. There s clearly no reason to add multiple operators for the same function.

使用<条码>>>和<条码>和>及>;的理由可能回溯至双向操作。 如果你代表一些行业的分支,那么这些企业就是你们使用的工会和教派。

与工会和<代码>-<>>-挂钩的简单的斜线是确定差异。

Because set difference is a very useful and commonly known concept, but there s no (universally used) concept of „set addition“.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签