python 内置数据结构的基本操作 —— tuple(2)
2017-05-09 10:05
323 查看
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).
Tuples may be constructed in a number of ways:
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() or tuple(iterable)
The constructor builds a tuple whose items are the same and in the same order as iterable‘s items. iterable may 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’) and tuple( [1, 2, 3] ) returns (1, 2, 3). If no argument is given, the constructor creates a new empty tuple, ().
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, while f((a, b, c)) is a function call with a 3-tuple as the sole argument.
Tuples implement all of the common sequence operations.
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.
class tuple([iterable])
Tuples may be constructed in a number of ways:
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() or tuple(iterable)
The constructor builds a tuple whose items are the same and in the same order as iterable‘s items. iterable may 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’) and tuple( [1, 2, 3] ) returns (1, 2, 3). If no argument is given, the constructor creates a new empty tuple, ().
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, while f((a, b, c)) is a function call with a 3-tuple as the sole argument.
Tuples implement all of the common sequence operations.
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.
相关文章推荐
- python 内置数据结构的基本操作 —— Set(1)
- python 内置数据结构的基本操作 —— list(2)
- python 内置数据结构的基本操作 —— dict(2)
- python 内置数据结构的基本操作 —— dict(1)
- python 内置数据结构的基本操作 —— Set(2)
- python 内置数据结构的基本操作 —— tuple(1)
- python 内置数据结构的基本操作 —— list(2)
- python数据结构之列表基本操作[学习笔记]
- python数据结构-序列\序列通用操作\列表(基本操作、方法)
- Python中的list、dict、tuple、string基本操作
- Python内置数据结构——列表list,元组tuple
- Python实现基本数据结构中栈的操作示例
- Python实现基本数据结构---队列操作
- Python中list(列表)、tuple(元组)、dict(字典)基本操作快速入门
- python数据结构之链表——基本操作实现
- 5.Python基本数据结构-字典-创建/访问/基本操作/格式化输出
- python 序列(list,tuple,str)基本操作
- week03_python内置数据结构_set及操作
- Python实现基本数据结构---栈操作
- Python 数据结构基本操作