元组/列表之间的区别是什么?它们的优点/缺点是什么?
当前回答
列表是同构序列,而元组是异构数据结构。
其他回答
关键的区别是元组是不可变的。这意味着一旦创建了元组,就不能更改其中的值。
因此,如果您需要更改值,请使用List。
元组的好处:
性能略有改善。 元组是不可变的,可以用作字典中的键。 如果你不能改变它,其他人也不能,也就是说你不需要担心任何API函数等改变你的元组没有被要求。
正如人们已经提到的区别,我将写一下为什么是元组。
为什么首选元组?
小元组的分配优化 为了减少内存碎片和加快分配速度,Python重用旧元组。如果一个 元组不再需要,并且只有少于20个项而不是删除 Python会永久地将它移到一个空闲列表中。 一个空闲列表被分为20个组,每个组代表一个 长度为n的元组列表,从0到20。每组都可以储存 到2000个元组。第一个(零)组只包含一个元素和 表示空元组。
>>> a = (1,2,3)
>>> id(a)
4427578104
>>> del a
>>> b = (1,2,4)
>>> id(b)
4427578104
在上面的例子中,我们可以看到a和b有相同的id。这是 因为我们立即占用了一个被破坏的元组 空闲列表。 列表的分配优化 由于列表可以被修改,Python不会像元组那样使用相同的优化方法。然而, Python列表也有一个free列表,但它只用于空列表 对象。如果一个空列表被GC删除或收集,它可以被删除 以后重用。
>>> a = []
>>> id(a)
4465566792
>>> del a
>>> b = []
>>> id(b)
4465566792
来源:https://rushter.com/blog/python-lists-and-tuples/
为什么元组比列表更高效?- > https://stackoverflow.com/a/22140115
列表用于循环,元组用于结构。“%s %s”%元组。
列表通常是同构的,元组通常是异构的。
列表是可变长度的,元组是固定长度的。
list和tuple的区别
Literal someTuple = (1,2) someList = [1,2] Size a = tuple(range(1000)) b = list(range(1000)) a.__sizeof__() # 8024 b.__sizeof__() # 9088 Due to the smaller size of a tuple operation, it becomes a bit faster, but not that much to mention about until you have a huge number of elements. Permitted operations b = [1,2] b[0] = 3 # [3, 2] a = (1,2) a[0] = 3 # Error That also means that you can't delete an element or sort a tuple. However, you could add a new element to both list and tuple with the only difference that since the tuple is immutable, you are not really adding an element but you are creating a new tuple, so the id of will change a = (1,2) b = [1,2] id(a) # 140230916716520 id(b) # 748527696 a += (3,) # (1, 2, 3) b += [3] # [1, 2, 3] id(a) # 140230916878160 id(b) # 748527696 Usage As a list is mutable, it can't be used as a key in a dictionary, whereas a tuple can be used. a = (1,2) b = [1,2] c = {a: 1} # OK c = {b: 1} # Error
list和tuple的区别
元组和列表在Python中看起来都是相似的序列类型。
Literal syntax We use parenthesis () to construct tuples and square brackets [ ] to get a new list. Also, we can use call of the appropriate type to get required structure — tuple or list. someTuple = (4,6) someList = [2,6] Mutability Tuples are immutable, while lists are mutable. This point is the base the for the following ones. Memory usage Due to mutability, you need more memory for lists and less memory for tuples. Extending You can add a new element to both tuples and lists with the only difference that the id of the tuple will be changed (i.e., we’ll have a new object). Hashing Tuples are hashable and lists are not. It means that you can use a tuple as a key in a dictionary. The list can't be used as a key in a dictionary, whereas a tuple can be used tup = (1,2) list_ = [1,2] c = {tup : 1} # ok c = {list_ : 1} # error Semantics This point is more about best practice. You should use tuples as heterogeneous data structures, while lists are homogenous sequences.
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录