博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List vs tuple,何时使用? [重复]
阅读量:2381 次
发布时间:2019-05-10

本文共 4832 字,大约阅读时间需要 16 分钟。

本文翻译自:

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

  • 17 answers 17个答案

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楼

参考:


#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楼

are fixed size in nature whereas 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: 资料来源:


#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 . 它们总是可以轻松升级为 。

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 . 另请查看列表和元组的章节。

转载地址:http://vbexb.baihongyu.com/

你可能感兴趣的文章
Runnable和Callable有什么区别
查看>>
Java多线程的start()方法和run()方法
查看>>
MySQL面试题——数据库基础知识
查看>>
MySQL面试题——数据类型
查看>>
MySQL面试题——索引
查看>>
MySQL面试题——索引的数据结构
查看>>
B树相关
查看>>
MySQL面试题——哈希索引
查看>>
MySQL面试题——索引算法
查看>>
MySQL面试题——索引设计和创建的原则
查看>>
MySQL面试题——创建索引的三种方式
查看>>
MySQL面试题——创建索引时需要注意什么
查看>>
MySQL面试题——前缀索引
查看>>
MySQL面试题——使用B-Tree和B+Tree各自的好处
查看>>
MySQL面试题——数据库为什么使用B+树而不是B树
查看>>
SpringMVC——DispatcherServlet源码分析
查看>>
Java并发编程面试题——阻塞态有哪几种
查看>>
2020-08-05
查看>>
sleep()和wait()有什么区别
查看>>
MySQL面试题——聚簇索引和非聚簇索引
查看>>