是Python list.extend()顺序加压吗? [英] Is Python list.extend() Order Presserving?

查看:58
本文介绍了是Python list.extend()顺序加压吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道扩展函数是否保留两个列表中的顺序.

I'm wondering whether the extend function preserves the order in the two list.

>> list = [1, 2, 3]
>> list.extend([4, 5])
>> list
[1, 2, 3, 4, 5]

扩展是否总是那样工作?

Is extend always working like that way?

推荐答案

是.

list.extend()扩展赋予列表末尾的参数.

list.extend() just extends the arguments given to the end of the list.

根据文档:

通过添加给定列表中的所有项目来扩展列表;等效于a [len(a):] = L.

Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

所以:

>>> a = [1, 2, 3]
>>> a[len(a):] = [4, 5]
>>> a
[1, 2, 3, 4, 5]


顺便说一句,不要通过命名列表 list 来隐藏内置类型.

这篇关于是Python list.extend()顺序加压吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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