==的语义 [英] Semantics of ==
问题描述
仍然试图理解==......如果对它们执行相同的操作,似乎两个相等的对象
会变得不相等:
Still trying to understand "=="... It appears as if two equal objects
can become unequal if you perform the same operations on them:
l = [1]
s = l
l.append(s)
w = [ 1]
r = [1,w]
w.append(r)
s
[1,[...]] w
[1,[ 1,[...]]] s == w
真实
请注意,它们相同,但打印方式不同。
s [0] = 2
w [0] = 2
s == w
l=[1]
s=l
l.append(s)
w=[1]
r=[1,w]
w.append(r)
s [1, [...]] w [1, [1, [...]]] s==w True
Note that they''re equal, yet are printed differently.
s[0]=2
w[0]=2
s==w
False
突然间他们变得不平等了。
Axel
False
All of a sudden they have become unequal.
Axel
推荐答案
Axel Boldt <斧******* @ yahoo.com>在消息中写道
news:40 ************************** @ posting.google.c om ...
"Axel Boldt" <ax*******@yahoo.com> wrote in message
news:40**************************@posting.google.c om...
仍然试图理解==......如果对它们执行相同的操作,似乎两个相等的对象可能变得不相等:
Still trying to understand "=="... It appears as if two equal objects
can become unequal if you perform the same operations on them:
>>> l = [1]
>>> s = l
>>> l.append(s)
>>> w = [1]
>>> r = [1,w]
>>> w.append(r)
>>> s [1,[...]]>>> w [1,[1,[...]]]>>> s == w True
请注意,它们是相同的,但打印方式不同。
>>> s [0] = 2
>>> w [0] = 2
>>> s == w
>>> l=[1]
>>> s=l
>>> l.append(s)
>>> w=[1]
>>> r=[1,w]
>>> w.append(r)
>>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
Note that they''re equal, yet are printed differently.
>>> s[0]=2
>>> w[0]=2
>>> s==w
错误
突然间他们变得不平等了。
Axel
False
All of a sudden they have become unequal.
Axel
>
你有一个递归结构!我原本以为
,这是你打印输出的东西,
但是它不是。
我认为原来的True是一个bug。通过递归会让人感到困惑
。
John Roth
You''ve got a recursive structure! I originally thought
that the [...] was something you''d done to the printout,
but it isn''t.
I think the original True is a bug. It''s getting confused
by the recursion.
John Roth
Axel Boldt写道:
Axel Boldt wrote:
仍然试图理解==......如果对它们执行相同的操作,似乎两个相等的对象可能变得不相等:< br>
Still trying to understand "=="... It appears as if two equal objects
can become unequal if you perform the same operations on them:
>>> l = [1]
>>> s = l
>>> l.append(s)
>>> w = [1]
>>> r = [1,w]
>>> w.append(r)
>>> s [1,[...]]>>> w [1,[1,[...]]]>>> s == w True
您正在创建递归列表(这在真正的
世界中极为罕见;我从未见过有人创建过
现实代码中的自引用列表。树结构甚至递归数据结构都是一件事,但你在考虑一个非常奇怪的角落案例。
请注意,它们是相同的,但打印方式不同。
>>> l=[1]
>>> s=l
>>> l.append(s)
>>> w=[1]
>>> r=[1,w]
>>> w.append(r)
>>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
You''re creating recursive lists (which is extremely rare in the real
world; I''ve never seen anyone create a self-referencing list in
real-life code. Tree structures and even recursive data structures are
one thing, but you''re considering a very weird corner case here.
Note that they''re equal, yet are printed differently.
这没关系.1和1.0的打印方式不同(甚至是不同类型的
)但是它们是相同的。如果你尝试运行Python用来确定列表相等性的元素b
你会发现它们在这一点上实际上是相等的。第一个元素
是1,第二个元素是第一个元素是1的列表,第二个
该列表的元素是一个列表,其第一个元素是1,无限广。
That''s fine. 1 and 1.0 are printed differently (and even are of
different types) but they''re equal. If you try running through the
element-wise algorithm that Python uses to determine equality of lists,
you''ll see that they are in fact equal at this point. The first element
is 1, the second element is a list whose first element is 1, the second
element of that list is a list whose first element is 1, ad infinitum.
>>> s [0] = 2
>>> w [0] = 2
>>> s == w False
突然间他们变得不平等。
>>> s[0]=2
>>> w[0]=2
>>> s==w False
All of a sudden they have become unequal.
因为现在他们真的不平等。显示它们:
Because now they really are not equal. Show them:
s
[2,[...]] w
[2,[1,[...]]] >
[...]并不表示递归包含哪个子列表,所以
你需要深入挖掘:
s [ 1]
[2,[...]] w [1] [1]
s [2, [...]] w [2, [1, [...]]]
The [...] doesn''t indicate which sublist is recursively contained, so
you have to dig deeper:
s[1] [2, [...]] w[1][1]
[2,[1,[。 ..]]]
所以s是
[2,[2,[2,[2,...]]] ]
但是w是
[2,[1,[2,[1,...]]] >
尽管两者都在递归,但很明显这些列表不是元素相等的,因此Python完全正确地说它们不是。
再一次,实际创建的自引用列表是极其罕见的。
-
__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE
\ __ /这里与夜晚打架。
- (维克多·雨果的最后一句话)
[2, [1, [...]]]
So s is
[2, [2, [2, [2, ...]]]]
but w is
[2, [1, [2, [1, ...]]]]
Despite both recursing, it''s clear that these lists are not element-wise
equal, and so Python is completely right in saying they''re not.
Again, though, real-world creation of self-referencing lists is
extremely rare at best.
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Here day fights with night.
-- (the last words of Victor Hugo)
>
John Roth写道:
John Roth wrote:
" Axel Boldt" <斧******* @ yahoo.com>在消息中写道
新闻:40 ************************** @ posting.google.c om ...
"Axel Boldt" <ax*******@yahoo.com> wrote in message
news:40**************************@posting.google.c om...
仍然试图理解==......如果对它们执行相同的操作,似乎两个相等的对象
会变得不相等:
Still trying to understand "=="... It appears as if two equal objects
can become unequal if you perform the same operations on them:
>>> l = [1]
>>> s = l
>>> l.append(s)
>>> w = [1]
>>> r = [1,w]
>>> w.append(r)
>>> s
>>> l=[1]
>>> s=l
>>> l.append(s)
>>> w=[1]
>>> r=[1,w]
>>> w.append(r)
>>> s
[1,[...]]
[1, [...]]
>>> w
>>> w
[1,[1,[...]]]
[1, [1, [...]]]
>>> s == w
>>> s==w
确实
请注意,它们是相同的,但打印方式不同。
True
Note that they''re equal, yet are printed differently.
>> > s [0] = 2
>>> w [0] = 2
>>> s == w
>>> s[0]=2
>>> w[0]=2
>>> s==w
错误
突然间他们变得不平等了。
Axel
False
All of a sudden they have become unequal.
Axel
>你有一个递归结构!我原本以为[...]是你打印出来的东西,
但是它不是。
我认为原来的真是一个bug。递归已经让人感到困惑。
You''ve got a recursive structure! I originally thought
that the [...] was something you''d done to the printout,
but it isn''t.
I think the original True is a bug. It''s getting confused
by the recursion.
Nah,Python完全就在这里。
原来的s和w有额外的有限元素并不能使它们不等于
。遵循Python的元素相等算法,你会看到它们必须相等。原来的是
Nah, Python''s completely right here.
That the original s and w have extra finite elements doesn''t make them
unequal. Follow Python''s elementwise equality algorithm down and you''ll
see they must be equal. The original s is
l = [1]
s = l
l。追加(s)
s
[1,[...]]
所以它是
[ 1,[1,[1,[1,[1,...]]]]]
原始w是
w = [1]
r = [1,w]
w.append(r)
w
[1,[1,[...]]]
>
所以它也是
[1,[1,[1,[1,[1,...]]]]]
你真正的奇怪之处在于递归的子列表不是附加的
,而是附加的:
a = [1]
a.insert (0,a)
a
l = [1]
s = l
l.append(s)
s [1, [...]]
so it is
[1, [1, [1, [1, [1, ...]]]]]
The original w is
w = [1]
r = [1, w]
w.append(r)
w [1, [1, [...]]]
so it is also
[1, [1, [1, [1, [1, ...]]]]]
The real weirdness you reach is when the recursive sublists are not
appended, but prepended:
a = [1]
a.insert(0, a)
a
[[...],1]
在这里,你甚至无法解决第一个元素进行比较!
-
__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE
\ __ /这里与夜晚打架。
- (维克多·雨果的最后一句话)
[[...], 1]
Here you could never even resolve a first element for comparison!
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Here day fights with night.
-- (the last words of Victor Hugo)
>
这篇关于==的语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!