在保留原件的同时传递列表

时间:2022-05-17 02:12:59

So I'm teaching myself Python, and I'm having an issue with lists. I want to pass my function a list and pop items off it while retaining the original list. How do I make python "instance" the passed list rather that passing a pointer to the original one?

所以我在教自己Python,我遇到了列表问题。我希望将我的函数传递给列表,并在保留原始列表的同时弹出项目。如何让python“instance”成为传递的列表,而不是将指针传递给原始列表?

Example:

def burninate(b):
    c = []
    for i in range(3):
        c.append(b.pop())
    return c

a = range(6)
d = burninate(a)
print a, d

Output: [0, 1, 2] [5, 4, 3]
Desired output: [0, 1, 2, 3, 4, 5] [5, 4, 3]

输出:[0,1,2] [5,4,3]期望输出:[0,1,2,3,4,5] [5,4,3]

Thanks!

7 个解决方案

#1


14  

As other answers have suggested, you can provide your function with a copy of the list.

正如其他答案所示,您可以为您的函数提供列表的副本。

As an alternative, your function could take a copy of the argument:

作为替代方案,您的函数可以获取参数的副本:

def burninate(b):
    c = []
    b = list(b)
    for i in range(3):
        c.append(b.pop())
    return c

Basically, you need to be clear in your mind (and in your documentation) whether your function will change its arguments. In my opinion, functions that return computed values should not change their arguments, and functions that change their arguments should not return anything. See python's [].sort(), [].extend(), {}.update(), etc. for examples. Obviously there are exceptions (like .pop()).

基本上,你需要在脑海中(以及在你的文档中)清楚你的功能是否会改变它的论点。在我看来,返回计算值的函数不应该更改它们的参数,并且更改其参数的函数不应返回任何内容。有关示例,请参阅python的[] .sort(),[]。extend(),{}。update()等。显然有例外(如.pop())。

Also, depending on your particular case, you could rewrite the function to avoid using pop() or other functions that modify the argument. e.g.

此外,根据您的具体情况,您可以重写该函数以避免使用pop()或其他修改参数的函数。例如

def burninante(b):
    return b[:-4:-1]   # return the last three elements in reverse order

#2


10  

You can call burninate() with a copy of the list like this:

您可以使用列表的副本调用burninate(),如下所示:

d = burninate(a[:])

d = burninate(a [:])

or,

d = burninate(list(a))

d = burninate(列表(a))

The other alternative is to make a copy of the list in your method:

另一种方法是在方法中复制列表:

def burninate(b):
    c=[]
    b=b[:]
    for i in range(3):
        c.append(b.pop())
    return c

>>> a = range(6)
>>> b = burninate(a)
>>> print a, b
>>> [0, 1, 2, 3, 4, 5] [5, 4, 3]

#3


6  

A slightly more readable way to do the same thing is:

一个稍微更易读的方法来做同样的事情是:

d = burninate(list(a))

Here, the list() constructor creates a new list based on a.

这里,list()构造函数基于a创建一个新列表。

#4


5  

A more general solution would be to import copy, and use copy.copy() on the parameter.

更通用的解决方案是导入副本,并对参数使用copy.copy()。

#5


2  

Other versions:

def burninate(b):
    c = []
    for i in range(1, 4):
        c.append(b[-i])
    return c
def burninate(b):
    c = b[-4:-1]
    c.reverse()
    return c

And someday you will love list comprehensions:

总有一天你会喜欢列表理解:

def burninate(b):
    return [b[-i] for i in range(1,4)]

#6


1  

You can use copy.deepcopy()

你可以使用copy.deepcopy()

#7


0  

burninate = lambda x: x[:-4:-1]

#1


14  

As other answers have suggested, you can provide your function with a copy of the list.

正如其他答案所示,您可以为您的函数提供列表的副本。

As an alternative, your function could take a copy of the argument:

作为替代方案,您的函数可以获取参数的副本:

def burninate(b):
    c = []
    b = list(b)
    for i in range(3):
        c.append(b.pop())
    return c

Basically, you need to be clear in your mind (and in your documentation) whether your function will change its arguments. In my opinion, functions that return computed values should not change their arguments, and functions that change their arguments should not return anything. See python's [].sort(), [].extend(), {}.update(), etc. for examples. Obviously there are exceptions (like .pop()).

基本上,你需要在脑海中(以及在你的文档中)清楚你的功能是否会改变它的论点。在我看来,返回计算值的函数不应该更改它们的参数,并且更改其参数的函数不应返回任何内容。有关示例,请参阅python的[] .sort(),[]。extend(),{}。update()等。显然有例外(如.pop())。

Also, depending on your particular case, you could rewrite the function to avoid using pop() or other functions that modify the argument. e.g.

此外,根据您的具体情况,您可以重写该函数以避免使用pop()或其他修改参数的函数。例如

def burninante(b):
    return b[:-4:-1]   # return the last three elements in reverse order

#2


10  

You can call burninate() with a copy of the list like this:

您可以使用列表的副本调用burninate(),如下所示:

d = burninate(a[:])

d = burninate(a [:])

or,

d = burninate(list(a))

d = burninate(列表(a))

The other alternative is to make a copy of the list in your method:

另一种方法是在方法中复制列表:

def burninate(b):
    c=[]
    b=b[:]
    for i in range(3):
        c.append(b.pop())
    return c

>>> a = range(6)
>>> b = burninate(a)
>>> print a, b
>>> [0, 1, 2, 3, 4, 5] [5, 4, 3]

#3


6  

A slightly more readable way to do the same thing is:

一个稍微更易读的方法来做同样的事情是:

d = burninate(list(a))

Here, the list() constructor creates a new list based on a.

这里,list()构造函数基于a创建一个新列表。

#4


5  

A more general solution would be to import copy, and use copy.copy() on the parameter.

更通用的解决方案是导入副本,并对参数使用copy.copy()。

#5


2  

Other versions:

def burninate(b):
    c = []
    for i in range(1, 4):
        c.append(b[-i])
    return c
def burninate(b):
    c = b[-4:-1]
    c.reverse()
    return c

And someday you will love list comprehensions:

总有一天你会喜欢列表理解:

def burninate(b):
    return [b[-i] for i in range(1,4)]

#6


1  

You can use copy.deepcopy()

你可以使用copy.deepcopy()

#7


0  

burninate = lambda x: x[:-4:-1]