Python 如何允许列表 append() 方法返回新列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12902980/
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-18 12:07:29  来源:igfitidea点击:

How to allow list append() method to return the new list

pythonlistappend

提问by Wilk

I want to do something like this:

我想做这样的事情:

myList = [10,20,30]
yourList = myList.append (40)

Unfortunately, list append does not return the modified list.

不幸的是,list append 不会返回修改后的列表。

So, how can I allow appendto return the new list?

那么,我怎样才能允许append返回新列表?

采纳答案by Martijn Pieters

Don't use append but concatenation instead:

不要使用附加而是串联:

yourList = myList + [40]

This returns a newlist; myListwill not be affected. If you need to have myListaffected as welleither use .append()anyway, then assign yourListseparately from (a copy of) myList.

这将返回一个列表;myList不会受到影响。如果您需要myList影响以及.append()无论如何使用,然后yourList分别从 (a copy of)分配myList

回答by inspectorG4dget

list.appendis a built-in and therefore cannot be changed. But if you're willing to use something other than append, you could try +:

list.append是内置的,因此无法更改。但是,如果您愿意使用 以外的东西append,您可以尝试+

In [106]: myList = [10,20,30]

In [107]: yourList = myList + [40]

In [108]: print myList
[10, 20, 30]

In [109]: print yourList
[10, 20, 30, 40]

Of course, the downside to this is that a new list is created which takes a lot more time than append

当然,这样做的缺点是创建了一个新列表,这比 append

Hope this helps

希望这可以帮助

回答by Storstamp

You only need to do myList.append(40)

你只需要做 myList.append(40)

It will append it to the original list, not return a new list.

它会将其附加到原始列表中,而不是返回新列表。

回答by Marcin Wyszynski

You can subclass the built-in list type and redefine the 'append' method. Or even better, create a new one which will do what you want it to do. Below is the code for a redefined 'append' method.

您可以继承内置列表类型并重新定义 'append' 方法。或者更好的是,创建一个新的来做你想要它做的事情。下面是重新定义的“追加”方法的代码。

#!/usr/bin/env python

class MyList(list):

  def append(self, element):
    return MyList(self + [element])


def main():
  l = MyList()
  l1 = l.append(1)
  l2 = l1.append(2)
  l3 = l2.append(3)
  print "Original list: %s, type %s" % (l, l.__class__.__name__)
  print "List 1: %s, type %s" % (l1, l1.__class__.__name__)
  print "List 2: %s, type %s" % (l2, l2.__class__.__name__)
  print "List 3: %s, type %s" % (l3, l3.__class__.__name__)


if __name__ == '__main__':
  main()

Hope that helps.

希望有帮助。

回答by jsears

Try using itertools.chain(myList, [40]). That will return a generator as a sequence, rather than allocating a new list. Essentially, that returns all of the elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.

尝试使用itertools.chain(myList, [40]). 这将返回一个生成器作为一个序列,而不是分配一个新列表。从本质上讲,这将返回第一个迭代中的所有元素,直到用完为止,然后继续下一个迭代,直到所有迭代都用完为止。

回答by ajayinvictus10

Just to expand on Storstamp's answer

只是为了扩展 Storstamp 的答案

You only need to do myList.append(40)

你只需要做 myList.append(40)

It will append it to the original list,now you can return the variable containing the original list.

它会将其附加到原始列表中,现在您可以返回包含原始列表的变量。

If you are working with very large lists this is the way to go.

如果您正在处理非常大的列表,这就是要走的路。

回答by Damian Paszkowski

In python 3 you may create new list by unpacking old one and adding new element:

在 python 3 中,您可以通过解压旧列表并添加新元素来创建新列表:

a = [1,2,3]
b = [*a,4] # b = [1,2,3,4] 

when you do:

当你这样做时:

myList + [40]

You actually have 3 lists.

你实际上有 3 个列表。