如何允许列表append()方法返回新列表 [英] How to allow list append() method to return the new list

查看:58
本文介绍了如何允许列表append()方法返回新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

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

不幸的是,列表追加未返回修改后的列表.

Unfortunately, list append does not return the modified list.

那么,如何允许append返回新列表?

So, how can I allow append to return the new list?

推荐答案

不使用附加,而是使用串联:

Don't use append but concatenation instead:

yourList = myList + [40]

这将返回一个 new 列表; myList将不受影响.如果还需要myList受影响的 ,也可以使用.append(),然后将yourListmyList(的副本)分开分配.

This returns a new list; myList will not be affected. If you need to have myList affected as well either use .append() anyway, then assign yourList separately from (a copy of) myList.

这篇关于如何允许列表append()方法返回新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆