是否保证Python列表的元素保持按插入顺序排列?

Python列表保证了元素的插入顺序,即使在进行操作如`a_list += [1,2,3]`时,也会保留顺序。整数在Python中是不可变的,所以不会改变。列表是可变的,可以改变其内容,但默认操作如添加元素会保持原有的顺序。要注意的是,`a_list.extend([1,2,3])`与`a_list = a_list + [1,2,3]`的区别,前者修改原列表,后者创建新的列表。" 107770240,9963643,织梦文章页TAG内链增强SEO与用户体验,"['织梦CMS', 'SEO优化', '内容管理系统', '内部链接', '网站优化']
摘要由CSDN通过智能技术生成

本文翻译自:Is a Python list guaranteed to have its elements stay in the order they are inserted in?

If I have the following Python code 如果我有以下Python代码

>>> x = []
>>> x = x + [1]
>>> x = x + [2]
>>> x = x + [3]
>>> x
[1, 2, 3]

Will x be guaranteed to always be [1,2,3] , or are other orderings of the interim elements possible? x将保证始终为[1,2,3] ,还是可能的其他临时元素排序?


#1楼

参考:https://stackoom.com/question/vSRW/是否保证Python列表的元素保持按插入顺序排列


#2楼

是的,python列表中元素的顺序是持久的。


#3楼

In short, yes, the order is preserved. 简而言之,是的,订单得以保留。 In long: 长期:

In general the following definitions will always apply to objects like lists: 通常,以下定义将始终适用于列表之类的对象:

A list is a collection of elements that can contain duplicate elements and has a defined order that generally does not change unless explicitly made to do so. 列表是可以包含重复元素的元素的集合,并且具有通常不会更改的已定义顺序,除非明确指示这样做。 stacks and queues are both types of lists that provide specific (often limited) behavior for adding and removing elements (stacks being LIFO, queues being FIFO). 堆栈队列都是列表类型,它们为添加和删除元素提供特定的(通常是有限的)行为(堆栈为LIF

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值