本文翻译自:Is there a standardized method to swap two variables in Python?
In Python, I've seen two variable values swapped using this syntax: 在Python中,我已经看到使用此语法交换了两个变量值:
left, right = right, left
Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped? 这是否被认为是交换两个变量值的标准方法,或者是否有其他一些方式可以按照惯例最通常地交换两个变量?
#1楼
参考:https://stackoom.com/question/10Fa0/是否有标准化的方法可以在Python中交换两个变量
#2楼
这是交换两个变量的标准方法,是的。
#3楼
Python evaluates expressions from left to right. Python从左到右计算表达式。 Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. 请注意,在评估分配时,右侧的评估先于左侧。
http://docs.python.org/3/reference/expressions.html#evaluation-order http://docs.python.org/3/reference/expressions.html#evaluation-order
That means the following for the expression a,b = b,a
: 这意味着表达式a,b = b,a
:
- the right-hand side
b,a
is evaluated, that is to say a tuple of two elements is created in the memory. 右边b,a
被求值,也就是说在内存中创建了两个元素的元组。 The two element are the objects designated by the identifiersb
anda
, that were existing before the instruction is encoutered during an execution of program 这两个元素是由标识符b
和a
指定的对象,它们在程序执行期间对指令进行加密之前就已存在 - just after the creation of this tuple, no assignement of this tuple object have still been made, but it doesn't matter, Python internally knows where it is 创建此元组后,仍未分配该元组对象,但这没关系,Python内部知道它在哪里
- then, the left-hand side is evaluated, that is to say the tuple is assigned to the left-hand side 然后,评估左侧,即将元组分配给左侧
- as the left-hand side is composed of two identifiers, the tuple is unpacked in order that the first identifier
a
be assigned to the first element of the tuple (which is the object that was formely b before the swap because it had nameb
) 因为左侧由两个标识符组成,所以对元组进行解压缩,以便将第一个标识符a
分配给元组的第一个元素(这是交换之前为b的对象,因为它的名称为b
)
and the second identifierb
is assigned to the second element of the tuple (which is the object that was formerly a before the swap because its identifiers wasa
) 并且第二个标识符b
分配给元组的第二个元素(这是交换之前的a对象,因为其标识符为a
)
This mechanism has effectively swapped the objects assigned to the identifiers a
and b
该机制有效地交换了分配给标识符a
和b
的对象
So, to answer your question: YES, it's the standard way to swap two identifiers on two objects. 因此,回答您的问题:是的,这是在两个对象上交换两个标识符的标准方法。
By the way, the objects are not variables, they are objects. 顺便说一下,对象不是变量,而是对象。
#4楼
I know three ways to swap variables, but a, b = b, a
is the simplest. 我知道三种交换变量的方法,但是a, b = b, a
是最简单的。 There is 有
XOR (for integers) XOR(整数)
x = x ^ y
y = y ^ x
x = x ^ y
Or concisely, 或简而言之,
x ^= y
y ^= x
x ^= y
Temporary variable 临时变量
w = x
x = y
y = w
del w
Tuple swap 元组交换
x, y = y, x
#5楼
I would not say it is a standard way to swap because it will cause some unexpected errors. 我不会说这是一种标准的交换方式,因为它将导致一些意外错误。
nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
nums[i]
will be modified first and then affect the second variable nums[nums[i] - 1]
. nums[i]
将首先被修改,然后影响第二个变量nums[nums[i] - 1]
。
#6楼
Does not work for multidimensional arrays, because references are used here. 不适用于多维数组,因为此处使用了引用。
import numpy as np
# swaps
data = np.random.random(2)
print(data)
data[0], data[1] = data[1], data[0]
print(data)
# does not swap
data = np.random.random((2, 2))
print(data)
data[0], data[1] = data[1], data[0]
print(data)
See also Swap slices of Numpy arrays 另请参见交换Numpy数组的切片