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

查看:38
本文介绍了如何允许列表 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]

这将返回一个列表;myList 不会受到影响.如果你需要 myList 也影响 要么使用 .append() ,然后分配 yourList 与(副本)myList.

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天全站免登陆