Python3 元组
元组运算符
len((1, 2, 3)) | 3 | 计算元素个数 |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | 连接 |
('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制 |
3 in (1, 2, 3) | True | 元素是否存在 |
for x in (1, 2, 3): print x, | 1 2 3 | 迭代 |
元组索引,截取
L[2] | 'Runoob!' | 读取第三个元素 |
L[-2] | 'Taobao' | 反向读取;读取倒数第二个元素 |
L[1:] | ('Taobao', 'Runoob!') | 截取元素,从第二个开始后的所有元素。 |
元组内置函数
1 | len(tuple) | >>> tuple1 = ('Google', 'Runoob', 'Taobao') >>> len(tuple1) 3 >>> |
2 | max(tuple) | >>> tuple2 = ('5', '4', '8') >>> max(tuple2) '8' >>> |
3 | min(tuple) | >>> tuple2 = ('5', '4', '8') >>> min(tuple2) '4' >>> |
4 | tuple(seq) | >>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu'] >>> tuple1=tuple(list1) >>> tuple1 ('Google', 'Taobao', 'Runoob', 'Baidu') |