是否有標準化的方法可以在Python中交換兩個變量?

本文翻譯自: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 identifiers b and a , that were existing before the instruction is encoutered during an execution of program 這兩個元素是由標識符ba指定的對象,它們在程序執行期間對指令進行加密之前就已存在
  • 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 name b ) 因爲左側由兩個標識符組成,所以對元組進行解壓縮,以便將第一個標識符a分配給元組的第一個元素(這是交換之前爲b的對象,因爲它的名稱爲b
    and the second identifier b is assigned to the second element of the tuple (which is the object that was formerly a before the swap because its identifiers was a ) 並且第二個標識符b分配給元組的第二個元素(這是交換之前的a對象,因爲其標識符爲a

This mechanism has effectively swapped the objects assigned to the identifiers a and b 該機制有效地交換了分配給標識符ab的對象

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數組的切片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章