以下内容主要摘抄博客:https://blog.csdn.net/yezonggang/article/details/50976664?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
一、tuple也是一个class,是不可变的list类型,不可以增删改。
创建:
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
访问:(与list一样)tup1[1:5];
修改:不可以修改,只能增加新的部分;
tup3 = tup1 + tup2;
print tup3;
注意:python中定义元组如:
a = ()
或者 a = (c_longlong*1024)()
二、任意无符号的对象,以逗号隔开,默认为元组,如下实例:
a=1,2,3,'e'
a=(1,2,3,'e').
三、Python元组包含了以下内置函数(与list差不多的函数)
1、cmp(tuple1, tuple2):比较两个元组元素。
2、len(tuple):计算元组元素个数。
3、max(tuple):返回元组中元素最大值。
4、min(tuple):返回元组中元素最小值。
5、tuple(seq):将列表转换为元组。
四、tuple的方法:
1、count():查找元素在tuple中出现的次数。
2.index():查找元素的第一个索引值。