【发布时间】:2015-11-03 13:37:45
【问题描述】:
我正在努力解决 Python(2 和 3)中的这种奇怪行为:
>>> a = [1, 2]
>>> a[a.index(1)], a[a.index(2)] = 2, 1
这会导致:
>>> a
[1, 2]
但是如果你写
>>> a = [1, 2]
>>> a[a.index(1)], a[a.index(2)] = x, y
其中x, y != 2, 1(可以是1, 1、2, 2、3, 5 等),这会导致:
>>> a == [x, y]
True
正如人们所期望的那样。为什么a[a.index(1)], a[a.index(2)] = 2, 1 不产生结果a == [2, 1]?
>>> a == [2, 1]
False
【问题讨论】:
-
你问了一个奇怪的问题。为什么
a[a.index(1)], a[a.index(2)] = 2, 1应该打印任何东西? :) -
我的意思是在请求 a 的值时。我在滥用符号。 XD
标签: python tuples variable-assignment