在 python 中交换列表元素,其中表达式包含函数调用
Swapping list elements in python where the expressions contain function calls
if arr = [4,3,2,1]
并且我想将第一个值与数组的最小值交换,如果我在 python
上使用它
arr[0] , arr[arr.index(min(arr))] = min(arr) , arr[0]
#or
arr[0] , arr[arr.index(min(arr))] = arr[arr.index(min(arr))] , arr[0]
它们不工作,但如果我这样做
b = arr.index(min(arr))
#and then
arr[0] , arr[b] = arr[b] , arr[0]
这很好用。谁能解释为什么?
跟操作顺序有关
我们可以对列表进行子类化以对其进行检测以显示它们在做什么。
class TracingList(list):
def __getitem__(self, key):
value = super().__getitem__(key)
print(self, "reading", key, "=", value)
return value
def __setitem__(self, key, value):
print(self, "writing", key, "=", value)
super().__setitem__(key, value)
def index(self, value):
index = super().index(value)
print(self, "finding index of", value, "=", index)
return index
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = min(arr), arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = arr[arr.index(min(arr))], arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
b = arr.index(min(arr))
arr[0], arr[b] = arr[b], arr[0]
print(arr)
打印出来
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] writing 3 = 4
[1, 3, 2, 4]
if arr = [4,3,2,1]
并且我想将第一个值与数组的最小值交换,如果我在 python
arr[0] , arr[arr.index(min(arr))] = min(arr) , arr[0]
#or
arr[0] , arr[arr.index(min(arr))] = arr[arr.index(min(arr))] , arr[0]
它们不工作,但如果我这样做
b = arr.index(min(arr))
#and then
arr[0] , arr[b] = arr[b] , arr[0]
这很好用。谁能解释为什么?
跟操作顺序有关
我们可以对列表进行子类化以对其进行检测以显示它们在做什么。
class TracingList(list):
def __getitem__(self, key):
value = super().__getitem__(key)
print(self, "reading", key, "=", value)
return value
def __setitem__(self, key, value):
print(self, "writing", key, "=", value)
super().__setitem__(key, value)
def index(self, value):
index = super().index(value)
print(self, "finding index of", value, "=", index)
return index
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = min(arr), arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
arr[0], arr[arr.index(min(arr))] = arr[arr.index(min(arr))], arr[0]
print(arr)
print("===")
arr = TracingList([4, 3, 2, 1])
b = arr.index(min(arr))
arr[0], arr[b] = arr[b], arr[0]
print(arr)
打印出来
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] finding index of 1 = 0
[1, 3, 2, 1] writing 0 = 4
[4, 3, 2, 1]
===
[4, 3, 2, 1] finding index of 1 = 3
[4, 3, 2, 1] reading 3 = 1
[4, 3, 2, 1] reading 0 = 4
[4, 3, 2, 1] writing 0 = 1
[1, 3, 2, 1] writing 3 = 4
[1, 3, 2, 4]