元组是不可变对象。python
t = tuple() t = () t = tuple(range(1,7,2)) t = (1,2,3,4,5,1) t = (1,) t = (1,)*5 t = (1,2,3)*6
支持下表索引code
正索引对象
负索引索引
tuple[index] t = (1,2,3) t[1] = 6 # 报错 u = (1,[2,3,4],5) u[1][1] = 10 # 能够改变
index(value) : 经过 value 从指定区间查询字符串
count(value) : 返回元组中匹配 value 的次数io
len() : 返回元素个数class
namedtuple(typename, field_names, verbose=False, rename=False)import
命名元组,返回一个元组的子类,并定义了字段im
field_names : 能够是以空格或逗号分隔的字符串命名
from collection import namedtuple Point = namedtuple('_Point', ['x','y']) p1 = Point(11,22) p1 # _Point(x=1,y=2) p1.x # 1 pa.y # 2 Student = namedtuple('Student','name age') tom = Student('tom', 20) jerry = Student('jerry', 18) tom.name