List vs tuple,何时使用? [重复]

本文翻译自:List vs tuple, when to use each? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

In Python, when should you use lists and when tuples? 在Python中,何时应该使用列表和何时使用元组?

Sometimes you don't have a choice, for example if you have 有时你没有选择权,例如你有

"hello %s you are %s years old" % x

then x must be a tuple. 然后x必须是一个元组。

But if I am the one who designs the API and gets to choose the data types, then what are the guidelines? 但如果我是设计API并选择数据类型的人,那么指导原则是什么?


#1楼

参考:https://stackoom.com/question/7ASc/List-vs-tuple-何时使用-重复


#2楼

I believe (and I am hardly well-versed in Python) that the main difference is that a tuple is immutable (it can't be changed in place after assignment) and a list is mutable (you can append, change, subtract, etc). 我相信(我并不精通Python),主要区别在于元组是不可变的 (在赋值后它不能被改变)并且列表是可变的 (你可以追加,改变,减去等等) )。

So, I tend to make my tuples things that shouldn't change after assignment and my lists things that can. 所以,我倾向于让我的元组在分配后不应该改变的东西,我列出的东西可以。


#3楼

Tuples are fixed size in nature whereas lists are dynamic. 元组本质上是固定大小的,而列表是动态的。
In other words, a tuple is immutable whereas a list is mutable . 换句话说, tuple不可变的,list可变的

  1. You can't add elements to a tuple. 您无法将元素添加到元组。 Tuples have no append or extend method. 元组没有追加或扩展方法。
  2. You can't remove elements from a tuple. 您无法从元组中删除元素。 Tuples have no remove or pop method. 元组没有删除或弹出方法。
  3. You can find elements in a tuple, since this doesn't change the tuple. 您可以在元组中找到元素,因为这不会更改元组。
  4. You can also use the in operator to check if an element exists in the tuple. 您还可以使用in运算符来检查元组中是否存在元素。

  • Tuples are faster than lists. 元组比列表更快。 If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list. 如果你要定义一组常量值,那么你将要做的就是迭代它,使用元组而不是列表。

  • It makes your code safer if you “write-protect” data that does not need to be changed. 如果您“写保护”不需要更改的数据,它会使您的代码更安全。 Using a tuple instead of a list is like having an implied assert statement that this data is constant, and that special thought (and a specific function) is required to override that. 使用元组而不是列表就像有一个隐含的断言声明,这个数据是常量,并且需要特殊思想(和特定函数)来覆盖它。

  • Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). 一些元组可以用作字典键(特别是包含不可变值的元组,如字符串,数字和其他元组)。 Lists can never be used as dictionary keys, because lists are not immutable. 列表永远不能用作字典键,因为列表不是不可变的。

Source: Dive into Python 3 资料来源: 深入研究Python 3


#4楼

Must it be mutable? 它必须是可变的吗? Use a list. 使用列表。 Must it not be mutable? 它一定不可变吗? Use a tuple. 使用元组。

Otherwise, it's a question of choice. 否则,这是一个选择问题。

For collections of heterogeneous objects (like a address broken into name, street, city, state and zip) I prefer to use a tuple. 对于异构对象的集合(如地址分为名称,街道,城市,州和邮编),我更喜欢使用元组。 They can always be easily promoted to named tuples . 它们总是可以轻松升级为命名元组

Likewise, if the collection is going to be iterated over, I prefer a list. 同样,如果集合将被迭代,我更喜欢列表。 If it's just a container to hold multiple objects as one, I prefer a tuple. 如果它只是一个容纳多个对象的容器,我更喜欢一个元组。


#5楼

There's a strong culture of tuples being for heterogeneous collections, similar to what you'd use struct s for in C, and lists being for homogeneous collections, similar to what you'd use arrays for. 对于异构集合,有一种强大的元组文化,类似于你在C中使用struct的类型,并且列出了同类集合,类似于你使用数组的集合。 But I've never quite squared this with the mutability issue mentioned in the other answers. 但我从来没有像其他答案中提到的可变性问题那样对此进行平衡。 Mutability has teeth to it (you actually can't change a tuple), while homogeneity is not enforced, and so seems to be a much less interesting distinction. 可变性对它有影响(你实际上不能改变一个元组),而同质性没有强制执行,因此似乎是一个不那么有趣的区别。


#6楼

The first thing you need to decide is whether the data structure needs to be mutable or not. 您需要决定的第一件事是数据结构是否需要是可变的。 As has been mentioned, lists are mutable, tuples are not. 如前所述,列表是可变的,元组不是。 This also means that tuples can be used for dictionary keys, wheres lists cannot. 这也意味着元组可以用于字典键,而列表则不能。

In my experience, tuples are generally used where order and position is meaningful and consistant. 根据我的经验,元组通常用于秩序和位置有意义且一致的地方。 For example, in creating a data structure for a choose your own adventure game, I chose to use tuples instead of lists because the position in the tuple was meaningful. 例如,在为选择自己的冒险游戏创建数据结构时,我选择使用元组而不是列表,因为元组中的位置是有意义的。 Here is one example from that data structure: 以下是该数据结构的一个示例:

pages = {'foyer': {'text' : "some text", 
          'choices' : [('open the door', 'rainbow'),
                     ('go left into the kitchen', 'bottomless pit'),
                     ('stay put','foyer2')]},}

The first position in the tuple is the choice displayed to the user when they play the game and the second position is the key of the page that choice goes to and this is consistent for all pages. 元组中的第一个位置是用户在玩游戏时显示的选项,第二个位置是选择所用页面的键,这对所有页面都是一致的。

Tuples are also more memory efficient than lists, though I'm not sure when that benefit becomes apparent. 元组也比列表更有内存效率,但我不确定这种好处何时变得明显。

Also check out the chapters on lists and tuples in Think Python . 另请查看Think Python中列表和元组的章节。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值