Python 中列表、序列和切片的区别?
这些内置 Python 数据类型:list、sequence 和 slice 之间有什么区别?在我看来,这三个本质上代表了 C++ 和 Java 所谓的数组。
What are the differences between these built-in Python data types: list, sequence and slice? As I see it, all three essentially represent what C++ and Java call array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你在你的问题中混合了非常不同的东西,所以我只回答一个不同的问题
你现在问的是Python中最重要的接口之一:
iterable
- 它基本上是你可以使用的任何东西,比如for elem in iterable
。iterable
有三个后代:sequence
、generator
和mapping
。序列是一个具有随机访问的可迭代对象。您可以请求序列中的任何项目,而不必消耗它之前的项目。使用此属性,您可以构建
切片
,它可以一次为您提供多个元素。切片可以为您提供一个子序列:seq[from:until]
和每第 n 个项目:seq[from:until:nth]
。list
、tuple
和str
都是序列。如果访问是通过键而不是整数位置完成的,则您有一个映射。
dict
是基本映射。最基本的可迭代对象是生成器。它不支持随机访问,因此不支持切片。您必须按照给出的顺序消耗所有物品。生成器通常仅在迭代它们时才创建它们的项目。创建
生成器
的常见方法是生成器表达式。它们看起来与列表理解完全相同,只是带有圆括号,例如(f(x) for x in y)
。调用使用yield
关键字的函数也会返回一个生成器。所有可迭代对象的通用适配器是迭代器。迭代器与它们支持的最基本类型(生成器)具有相同的接口。它们是通过在可迭代对象上调用
iter
来显式创建的,并在各种循环结构中隐式使用。You're mixing very different things in your question, so I'll just answer a different question
You are now asking about one of the most important interface in Python:
iterable
- it's basically anything you can use likefor elem in iterable
.iterable
has three descendants:sequence
,generator
andmapping
.A sequence is a iterable with random access. You can ask for any item of the sequence without having to consume the items before it. With this property you can build
slices
, which give you more than one element at once. A slice can give you a subsequence:seq[from:until]
and every nth item:seq[from:until:nth]
.list
,tuple
andstr
all are sequences.If the access is done via keys instead of integer positions, you have a mapping.
dict
is the basic mapping.The most basic iterable is a generator. It supports no random access and therefore no slicing. You have to consume all items in the order they are given. Generator typically only create their items when you iterate over them. The common way to create
generators
are generator expressions. They look exactly like list comprehension, except with round brackets, for example(f(x) for x in y)
. Calling a function that uses theyield
keyword returns a generator too.The common adapter to all iterables is the iterator.
iterators
have the same interface as the most basic type they support, agenerator
. They are created explicitly by callingiter
on a iterable and are used implicitly in all kinds of looping constructs.list
不仅仅是普通数组。您可以在不给出项目数量的情况下初始化它们。您可以附加
/push
到它们,您可以删除
/pop
/del
来自它们的项目,您可以拥有不同类型对象的列表(例如,[1,'e', [3]]
),您可以拥有递归列表...并且您可以对列表进行切片,这意味着得到一个只有少数项目的新列表。slice
是一种对象类型,用于“在幕后”处理a[start:stop:step]
形式的扩展切片,如help(slice)< /code> 揭示了。
“序列”不是一个对象,更像是一些对象(如
list
)实现的非正式接口。list
are more than plain arrays. You can initialize them without giving the number of items. You canappend
/push
to them, you canremove
/pop
/del
items from them, you can have lists of different types of objects (e.g.,[1,'e', [3]]
), you can have recursive lists... and you can slice lists, which means getting a new list with only a few of the items.slice
are an object type used "behind the scenes" to handle extended slicing in thea[start:stop:step]
form, ashelp(slice)
reveals."Sequence" is not an object, more like an informal interface some objects like
list
implement.列表是序列类型,类似于数组
序列 类型描述功能超集:
阅读更多... http ://docs.python.org/glossary.html
lists are a sequence type, similar to an array
sequence types describe a functional superset:
Read more ... http://docs.python.org/glossary.html
列表是一个序列,但序列不一定是列表。序列是支持序列接口(“协议”)的任何类型。这是通过鸭子类型而不是通过严格的继承层次结构来完成的。请注意,序列是容器,但容器不一定是序列。 (序列是顺序的!)
请参阅 http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange
切片对象通常是通过语法糖隐式创建的(
foo[2:5]
)并提供给容器类型特殊方法(例如__getitem__
),您可以重写这些方法。除非您创建自己的序列/容器,否则您通常不必处理切片。请参阅http://docs.python.org/reference/datamodel.html#specialnames
列表与数组相当。我不确定,但我认为它是在 cPython 中作为动态扩展数组实现的。然而,该接口使其更像是一个 C++ STL Vector,而不仅仅是一个普通的旧数组。
A list is a sequence but a sequence is not necessarily a list. A sequence is any type that supports the sequence interface ("protocol"). This is done by duck-typing rather than through a strict inheritance hierarchy. Note that sequences are containers, but containers are not necessarily sequences. (sequences are, well, sequential!)
See http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange
Slice objects are generally created implicitly via syntactic sugar (
foo[2:5]
) and provided to the container type special methods (such as__getitem__
) which you can override. You will generally not have to deal with slices unless if you create your own sequences/containers.See http://docs.python.org/reference/datamodel.html#specialnames
Lists are comparable to arrays. I'm not certain, but I think it's implemented in cPython as a dynamically expanding array. However, the interface makes it so that it's more like a C++ STL Vector than just a plain old array.
严格来说,切片是一种表示一系列索引的类型,例如开始、停止和步骤。切片根本不是容器类型。您可以使用切片来索引列表,从而生成一个新列表,该新列表是原始列表的子列表的副本。
列表与 C++ 数组的不同之处在于它们是异构的;元素不需要是同一类型。正如 MYYN 已经指出的那样,“序列”根本不是 Python 类型,而是各种内置类型的描述。
Strictly speaking, a slice is a type which represents a range of indices, e.g. a start, stop, and a step. A slice isn't a container type at all. You can use a slice to index an list, resulting in a new list which is a copy of a sublist of the original list.
Lists differ from C++ arrays in that they're heterogenous; the elements are not required to be of the same type. And as MYYN has already pointed out, "sequence" isn't a Python type at all but rather a description of a variety of built-in types.