4.6.5. Tuples
Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate()
built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set
or dict
instance).
翻译:元组是不可变的序列,经常被用来存储异构数据集(比如说 内置函数enumerate
(枚举)就会创建两个元素的元组,具体效果看下面的部分)。元组也经常在各种情况下使用,比如需要一个不可变序列数据(只读数据)(比如:允许被存放在set和dict中)
enumerate
(
iterable,
start=0
)
¶
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The __next__()
method of the iterator returned by enumerate()
returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
翻译:翻译一个enumerate 对象。iterable必须是一个序列,迭代器或者其他支持迭代的类型。enumerate会调用iterable的next方法,返回的值包含一个下标(默认从0开始)并且还有一个值,通过iterable的迭代获取。
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
Equivalent to:
这个方法等价于
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
-
class
tuple
( [ iterable ] ) -
Tuples may be constructed in a number of ways:
翻译:trutle可以通过下面几种方式进行构建实例。- Using a pair of parentheses to denote the empty tuple:
()
翻译:使用括号创建一个空的元组 - Using a trailing comma for a singleton tuple:
a,
or(a,)
翻译:在元素后面添加一个括号 - Separating items with commas:
a, b, c
or(a, b, c)
翻译:分隔元素使用逗号。 - Using the
tuple()
built-in:tuple()
ortuple(iterable)
翻译:使用tuple的构造方法,有两种:tuple() or tuple(iterable)
The constructor builds a tuple whose items are the same and in the same order as iterable’s items. iterablemay be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For example,
tuple('abc')
returns('a', 'b', 'c')
andtuple( [1, 2, 3] )
returns(1, 2, 3)
. If no argument is given, the constructor creates a new empty tuple,()
.
翻译:通过iterable构造的元组元素的顺序和元素内容以及地址都是一样的。iterable既可以是一个序列,支持迭代的容器,或者是一个iterator对象。如果iterable已经是一个元组了。那么他的返回将不会发生变化。比如说tuple('abc') returns ('a', 'b', 'c') and tuple( [1, 2, 3] )returns (1, 2, 3). 如果没有参数,将会构造一个空的元组。Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example,
f(a, b,c)
is a function call with three arguments, whilef((a, b, c))
is a function call with a 3-tuple as the sole argument.
翻译:值得注意的是,创建一个元组主要是逗号分隔而不是括号。括号是可选的,除了空元组以外。Tuples implement all of the common sequence operations.
翻译:元组实现了所有普通序列的基本操作。 - Using a pair of parentheses to denote the empty tuple:
For heterogeneous collections of data where access by name is clearer than access by index, collections.namedtuple()
may be a more appropriate choice than a simple tuple object.
翻译:对于异构数据(具有唯一性的数据)集合,通过name寻找很明显比通过索引寻找更加清晰明了。对于集合collections.namedtuple()
也许在这种场景下比普通的tuple集合更适用。
seasons = ['Spring', 'Summer', 'Fall', 'Winter'] enum = enumerate(seasons) for i in enum: print(i)输出
(0, 'Spring') (1, 'Summer') (2, 'Fall') (3, 'Winter')我们可以看到返回的是一个迭代的数据类型
namedTuple可以自行查阅,或者在后面将进行讲解。
from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(11, y=22) print(p[0] + p[1]) print(p) print(p.x,p.y) print(type(p),type(Point),type(int),type(1))
输出
33 Point(x=11, y=22) 11 22 <class '__main__.Point'> <class 'type'> <class 'type'> <class 'int'>
我们可以通过输出结果得出结论
Point是一种class类型,附属于main