列表和元组之间有区别吗? [英] Is there a difference between a list and a tuple?

查看:165
本文介绍了列表和元组之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了与特定编程语言有关的现有问题.特定语言在实现上有所不同,但是在理论上存在概念上的差异吗?

I see existing questions that relate to specific programming languages. There are implementation differences in specific languages, but is there a theoretical conceptual difference?

可变与不可变:在Python中,列表是完全可变的,而元组是不可变的或永久不可变的,因此修改会创建新的元组,而不进行就地修改.但这纯粹是实现细节.在其他语言中,元组是可变的,列表是不可变的.

Mutable vs immutable: In Python, lists are fully mutable while tuples are immutable or persistently immutable so that modifications create new tuples and do not do in place modifications. But this is purely an implementation detail. In other languages tuples are mutable and lists are immutable.

异构vs同类:在语义上,元组通常是异构的,而列表通常是同类的,但这更多是一种约定,并且有许多例外.像Python这样的动态类型语言具有异构列表.例如,Haskell支持称为HList的完全静态类型的异构列表.

Heterogeneous vs homogeneous: Semantically, tuples are usually heterogeneous, while lists are usually homogeneous, but this is more of a convention and there are many exceptions. Dynamically typed languages like Python have heterogeneous lists. Haskell, for example, has support for fully statically typed heterogeneous lists called HList.

有限与无限:从理论上讲,列表可以是无限的,并且某些编程语言(Haskell)支持无限列表.元组不能是无限的.

Finite vs Infinite: Theoretically, a list can be infinite, and some programming languages (Haskell) support infinite lists. A tuple can not be infinite.

更新:唯一的理论差异是元组必须是有限的,而列表在理论上可以是无限的.其余的差异只是纯粹的实现差异.

UPDATE: The only theoretical difference is that a tuple must be finite, while a list can theoretically be infinite. The rest of the differences are pure implementation differences.

维基百科说:元组是元素的有限有序列表."

Wikipedia says "A tuple is a finite ordered list of elements.".

这清楚地表明,元组是一个列表,但是是一个有限列表.

This makes it clear that a tuple is a list, but a finite list.

推荐答案

元组只是一个有限列表.所有元组都是列表.所有有限列表都是元组.无限列表不是元组.所有类型和语义上的差异都是纯粹依赖于语言的考虑因素.

A tuple is just a finite list. All tuples are lists. All finite lists are tuples. Infinite lists are not tuples. All differences of typing and semantics are purely language dependent considerations.

这篇关于列表和元组之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆