是否保证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). 堆栈队列都是列表类型,它们为添加和删除元素提供特定的(通常是有限的)行为(堆栈为LIFO,队列为FIFO)。 Lists are practical representations of, well, lists of things. 列表是事物列表的实际表示。 A string can be thought of as a list of characters, as the order is important ( "abc" != "bca" ) and duplicates in the content of the string are certainly permitted ( "aaa" can exist and != "a" ). 字符串可以被认为是一个字符列表,因为顺序很重要( "abc" != "bca" )并且字符串内容中的重复内容当然是允许的( "aaa"可以存在并且!= "a" )。

A set is a collection of elements that cannot contain duplicates and has a non-definite order that may or may not change over time. 集合是一组元素,它们不能包含重复项,并且具有可能会或可能不会随时间变化的非确定顺序。 Sets do not represent lists of things so much as they describe the extent of a certain selection of things. 因为他们描述事物有一定选择的程度集也不代表事情列出这么多。 The internal structure of set, how its elements are stored relative to each other, is usually not meant to convey useful information. 集合的内部结构,其元素如何相对于彼此存储,通常并不意味着传达有用的信息。 In some implementations, sets are always internally sorted; 在一些实现中,集合总是在内部排序; in others the ordering is simply undefined (usually depending on a hash function). 在其他情况下,排序只是未定义(通常取决于散列函数)。

Collection is a generic term referring to any object used to store a (usually variable) number of other objects. 集合是一个通用术语,指的是用于存储(通常是可变的)多个其他对象的任何对象。 Both lists and sets are a type of collection. 列表和集合都是一种集合。 Tuples and Arrays are normally not considered to be collections. 元组和数组通常不被视为集合。 Some languages consider maps (containers that describe associations between different objects) to be a type of collection as well. 有些语言也认为地图 (描述不同对象之间关联的容器)也是一种集合。

This naming scheme holds true for all programming languages that I know of, including Python, C++, Java, C#, and Lisp (in which lists not keeping their order would be particularly catastrophic). 这种命名方案适用于我所知道的所有编程语言,包括Python,C ++,Java,C#和Lisp(其中不保持订单的列表特别具有灾难性)。 If anyone knows of any where this is not the case, please just say so and I'll edit my answer. 如果有人知道任何情况并非如此,请说出来,我会编辑我的答案。 Note that specific implementations may use other names for these objects, such as vector in C++ and flex in ALGOL 68 (both lists; flex is technically just a re-sizable array). 请注意,特定实现可能会使用这些对象的其他名称,例如C ++中的vector和ALGOL 68中的flex (两个列表; flex在技术上只是一个可调整大小的数组)。

If there is any confusion left in your case due to the specifics of how the + sign works here, just know that order is important for lists and unless there is very good reason to believe otherwise you can pretty much always safely assume that list operations preserve order. 如果由于+符号在这里工作的具体情况而在你的案例中留下任何混淆,只要知道订单对于列表很重要 ,除非有充分的理由相信否则你几乎总能安全地假设列表操作保留订购。 In this case, the + sign behaves much like it does for strings (which are really just lists of characters anyway): it takes the content of a list and places it behind the content of another. 在这种情况下, +符号的行为与字符串(它实际上只是字符列表)的行为非常相似:它采用列表的内容并将其置于另一个列表的内容之后。

If we have 如果我们有

list1 = [0, 1, 2, 3, 4]
list2 = [5, 6, 7, 8, 9]

Then 然后

list1 + list2

Is the same as 是相同的

[0, 1, 2, 3, 4] + [5, 6, 7, 8, 9]

Which evaluates to 评估为

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Much like 很像

"abdcde" + "fghijk"

Produces 产生

"abdcdefghijk"

#4楼

I suppose one thing that may be concerning you is whether or not the entries could change, so that the 2 becomes a different number, for instance. 我想可能与你有关的一件事是,条目是否可以改变,例如,2变为不同的数字。 You can put your mind at ease here, because in Python, integers are immutable , meaning they cannot change after they are created. 你可以在这里放心,因为在Python中,整数是不可变的 ,这意味着它们在创建后不能改变。

Not everything in Python is immutable, though. 但是,并非Python中的所有内容都是不可变的。 For example, lists are mutable---they can change after being created. 例如,列表是可变的 - 它们可以在创建后更改。 So for example, if you had a list of lists 例如,如果您有一个列表列表

>>> a = [[1], [2], [3]]
>>> a[0].append(7)
>>> a
[[1, 7], [2], [3]]

Here, I changed the first entry of a (I added 7 to it). 在这里,我改变的第一条目a (I加入7到它)。 One could imagine shuffling things around, and getting unexpected things here if you are not careful (and indeed, this does happen to everyone when they start programming in Python in some way or another; just search this site for "modifying a list while looping through it" to see dozens of examples). 如果你不小心的话,可以想象一下这里的东西,并在这里得到意想不到的东西(事实上,当他们以某种方式开始用Python编程时,这确实发生在每个人身上;只是搜索这个网站“在循环时修改一个列表它“看到几十个例子”。

It's also worth pointing out that x = x + [a] and x.append(a) are not the same thing. 值得指出的是x = x + [a]x.append(a)并不是一回事。 The second one mutates x , and the first one creates a new list and assigns it to x . 第二个变异x ,第一个创建一个新列表并将其分配给x To see the difference, try setting y = x before adding anything to x and trying each one, and look at the difference the two make to y . 要看到差距,尝试设置y = x添加任何东西之前, x和尝试各一台,并期待在差两作出y


#5楼

aList=[1,2,3] ALIST = [1,2,3]

i=0 I = 0

for item in aList:  

    if i<2:  

            aList.remove(item)  

    i+=1  

aList 一个列表

[2] [2]

The moral is when modifying a list in a loop driven by the list, takes two steps: 道德是在修改由列表驱动的循环中的列表时,需要两个步骤:

aList=[1,2,3]
i=0
for item in aList:
    if i<2:
        aList[i]="del"
    i+=1

aList

['del', 'del', 3]
for i in range(2):
    del aList[0]

aList
[3]

#6楼

You are confusing 'sets' and 'lists'. 你会混淆'集'和'列表'。 A set does not guarantee order, but lists do. 套装不保证订单,但列表可以。

Sets are declared using curly brackets: {} . 使用大括号声明集合: {} In contrast, lists are declared using square brackets: [] . 相反,列表使用方括号声明: []

mySet = {a, b, c, c}

Does not guarantee order, but list does: 不保证订单,但列表确实:

myList = [a, b, c]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值