简体   繁体   English

Python如何在列表内交换元素?

[英]How Does Python Swap Elements inside a List?

I am new to Python, and just learned about mutable and immutable objects. 我是Python的新手,并且刚刚了解了可变和不可变的对象。 It appears that when we are swapping elements inside a list, Python creates copies of the individual elements and then copy them back into the list. 似乎当我们交换列表中的元素时,Python创建单个元素的副本,然后将它们复制回到列表中。

在此处输入图片说明

In the above example, we begin with n = [[1, 2], [3, 4]] . 在上面的示例中,我们从n = [[1, 2], [3, 4]] The first sublist, [1, 2] occupies ID# 24 , and the second sublist [3, 4] occupies ID# 96 . 第一个子列表[1, 2]占据ID# 24 ,第二个子列表[3, 4]占据ID# 96 At first, I thought that since lists are mutable, after swapping, ID# 24 holds [3, 4] and ID# 96 holds [1, 2] . 起初,我认为由于列表是可变的,因此在交换之后,ID# 24保持[3, 4] ,ID# 96保持[1, 2] But as we can see in the test above, that is not the case. 但是正如我们在上面的测试中看到的,情况并非如此。 Rather, Python is pointing to the objects in a manner determined by our swap; 相反,Python以我们交换决定的方式指向对象。 the first slot of the list points to 96 and the second slot points to 24 . 列表的第一个插槽指向96 ,第二个插槽指向24

Certainly, the ID# of n did not change, so n did not violate the definition of a mutable object. 当然, n的ID#不会改变,因此n不会违反可变对象的定义。 But the way the sublists got swapped seems to be a caveat. 但是子列表的交换方式似乎是一个警告。 Can you kindly confirm or explain in more precise language? 您能否以更精确的语言进行确认或解释?

You are mistaken about IDs. 您误认为ID。 The ID#24 is the ID of the list [1,2] . ID#24是列表[1,2]的ID。 It is NOT the memory address or ID of index 0 in n . 它不是n中索引0的内存地址或ID。

You might want to try an example like this to confirm: 您可能想尝试一个类似的示例来确认:

>>> x = [1, 2]
>>> id(x)
4332120256
>>> l = [x, 3]
>>> id(l[0])
4332120256
>>> 

From the documentation : 文档中

Assignment of an object to a target list is recursively defined as follows. 将对象分配给目标列表的定义如下。

If the target list is a single target: The object is assigned to that target. 如果目标列表是单个目标:将对象分配给该目标。 If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. 如果目标列表是目标的逗号分隔列表:该对象必须是可迭代的,并且具有与目标列表中存在目标的项目数量相同的项目,并且这些项目从左到右分配给相应的目标。

If you read that closely, you'll see that you have the scenario: 如果仔细阅读,将会发现您有这种情况:

target_list = expression_list

The expression list is evaluated first , and evaluates into a tuple: 表达式列表首先计算,并且评估成元组:

An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) 一个赋值语句评估表达式列表(请记住,它可以是单个表达式或逗号分隔的列表,后者产生一个元组)

So if you have: 因此,如果您有:

a = [1, 2]
a[1], a[0] = a[0], a[1]

It is effectively like writing the following: 它实际上就像编写以下内容:

a = [1, 2]
temp = (a[0], a[1]) # Expression list evaluates to tuple.

# target_list assignment to corresponding elements from expression list.
a[1] = temp[0]
a[0] = temp[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM