如何交换 Python 列表中的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27804020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:18:30  来源:igfitidea点击:

How can I swap items in a Python list?

pythonpython-2.7

提问by thisisashwani

The problem was to write the python code to generate all the permutations of the numbers {1,2,3,...,n}. So, I write this code:

问题是编写 python 代码来生成数字 {1,2,3,...,n} 的所有排列。所以,我写了这段代码:

def permute(n):

  if n==len(a):
    print a
    return

  for i in range(n,len(a)):
    swap(i,n)
    permute(n+1)
    swap(i,n)


def swap(x,y):
  a[x],a[y]=a[y],a[x]

a=[1,2,3,4]       #any list 
permute(0)

And it worked perfectly. But then thanks to the free time that I did have, I modified it a bit and wrote this code:

它工作得很好。但是后来由于我确实有空闲时间,我对其进行了一些修改并编写了以下代码:

def permute(n):

  if n==len(a):
    print a
    return

  for i in range(n,len(a)):
    swap(a[i],a[n])           #modification
    permute(n+1)
    swap(a[i],a[n])           #modification


def swap(x,y):
  x,y=y,x                     #modification

a=[1,2,3,4]
permute(0)

This time it didn't work. But after then, I read a bit about how the assigning of variables to values is different in python.

这次没用。但在那之后,我读了一些关于在 python 中将变量分配给值的不同之处。

But still I would like to know what, according to you, is wrong with the second code, so that I can cross-check and discuss what I think is going wrong! That's my first question.

但我仍然想知道,根据您的说法,第二个代码有什么问题,以便我可以交叉检查和讨论我认为哪里出了问题!这是我的第一个问题。

My second question is how the swapping of values takes place in a python list? Is it something different from what will happen with simple values? Because both the codes above seem to apply so. But I cannot figure out it a way I can make myself understand, plus it further confused me that then how python manipulates its lists.

我的第二个问题是如何在 python 列表中交换值?它与简单值会发生的情况有什么不同吗?因为上面的两个代码似乎都适用。但是我无法找到一种可以让自己理解的方式,而且让我更加困惑的是 python 如何操作它的列表。

I am sure there is something in the design of Python language that I don't know about leading to all these confusions. Help me sort them out and if possible, use somewhat picture visualizations. It would be very easy then for me to understand what's going on!!

我确信 Python 语言的设计中有一些我不知道的东西会导致所有这些混淆。帮我整理一下,如果可能的话,使用一些图片可视化。那时我很容易理解发生了什么!!

回答by Bhargav Rao

The swapfunction is swapping the local variables xand yand not the global array.

swap功能是交换的局部变量xy,而不是全局数组。

def swap(x,y):
  x,y=y,x 

This is completely wrong

这是完全错误的

You can instead do the following

您可以改为执行以下操作

  • Change the function body to

    return y,xand call the function as a[n],a[i] = swap(a[i],a[n])

  • Or directly swap in-place a[n],a[i] = a[i],a[n]

  • Or do it as you had done earlier
  • 将函数体更改为

    return y,x并将函数调用为 a[n],a[i] = swap(a[i],a[n])

  • 或者直接就地交换 a[n],a[i] = a[i],a[n]

  • 或者像你之前所做的那样做

回答by matsjoyce

First question:

第一个问题:

This is the reason:

这就是原因:

def swap(x,y):
    x, y = y, x

This just swaps the local names. xwill then equal y, and vice versa, but only inside that function. Outside that function (global scope), nothing will change.

这只是交换本地名称。x然后将等于y,反之亦然,但仅限于该函数内部。在该函数(全局范围)之外,什么都不会改变。



Second question (you should really avoid asking multiple questions in one question):

第二个问题(你真的应该避免在一个问题中问多个问题):

Assuming:

假设:

x = a[i]
y = a[n]

To swap values in a list, you must set them:

要交换列表中的值,您必须设置它们:

a[n] = x
a[i] = y

Which is that same as:

这与:

a[i], a[n] = y, x

And since y = a[n]and x = a[i], it is the same as:

由于y = a[n]x = a[i],它等同于:

a[i], a[n] = a[n], a[i]

回答by Márcio Santos Souza Car?so

If the list is composed only by numbers:

如果列表仅由数字组成:

A[i] = A[i] + A[n]
A[n] = A[i] - A[n]
A[i] = A[i] - A[n]