python数据类型之元组Tuple(二)
1.元组Tuple说明
元组是另一个数据类型,类似于 List(列表)。
元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
元组与字符串类似,下标索引从0开始,可以进行截取,组合等。
2.元组创建
#coding=utf-8 #元组的创建 #创建空的tuple t1=() #创建只有一个元素的元组,后面要加分号,不然视为数学公式中的小括号 t2=(2) t3=(2,) print(type(t1),len(t1),type(t2),len(str(t2)),type(t3),len(t3)) #当我们将整数传递给 len() 函数时,会出现 Python“TypeError: object of type ‘int’ has no len() ”。要解决该错误,需要将整数转换为字符串 #例如 len(str(t2)) 或更正分配并将序列(list, str 等)传递给 len() 函数。 #创建多个元素 t4=(1,"as",23,[1,2],"k") print(t4[3])
t3 = (2,); 这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号。所以,如果元组只有1个元素,就必须加一个逗号,防止被当作括号运算
运行结果:
<class 'tuple'> 0 <class 'int'> 1 <class 'tuple'> 1 [1, 2]
3.错误示例
t4[0] = 11; #元组无法被更新
4. 元组示例
#coding=utf-8 tuple1=(1,2,3,[1,2,"a",[4,8],1],5,7) tuple2=(1,2) tuple3=(1,2,3) tuple4=("a","b","c") #取值,类似list print(tuple1[:]) print(tuple1[0],tuple1[3][3],tuple1[-1],tuple1[2:4]) #删除元组 print(tuple2) del tuple2 # print(tuple2) 已删除,无法获取。报错 is not defined #拼接元组 print("tuple3+tuple4:",tuple3+tuple4) #求元组的元素个数 print(len(tuple3)) #复制元组 print(tuple4*2) #in not in 使用 print("d" in tuple4,"a" in tuple4,"k" not in tuple4) #迭代 for item in tuple3: print(item) #比较两个元组 #在python3.5版本,cpm(a,b)函数已被淘汰,可以用表达式(a > b) - (a < b)代替cmp(a,b) print((tuple4>tuple4)-(tuple4<tuple4)) #返回元组最大的值 print("max(tuple3):",max(tuple3)) #返回元组最小的值 print("min(tuple3):",min(tuple3)) #将列表转换为元组 list=[2,4,6,8] tuple5=tuple(list) print(type(list),type(tuple5),tuple5)
运行结果:
(1, 2, 3, [1, 2, 'a', [4, 8], 1], 5, 7) 1 [4, 8] 7 (3, [1, 2, 'a', [4, 8], 1]) (1, 2) tuple3+tuple4: (1, 2, 3, 'a', 'b', 'c') 3 ('a', 'b', 'c', 'a', 'b', 'c') False True True 1 2 3 0 max(tuple3): 3 min(tuple3): 1 <class 'list'> <class 'tuple'> (2, 4, 6, 8)
5.元组示例2
# coding=utf-8; #元组单个元素测试 #末尾无逗号 tuple1 = (1); #末尾有逗号 tuple2 = (1,); print "末尾无逗号:"+str(tuple1); print "末尾有逗号:"+str(tuple2); #元组无法被修改更新删除,但可以通过组合拼接的方式达到更新的目的 tuple3 = (1,2,3,4,5,6,7,8); tuple4 = ('A','B','C','D','E'); #组成一个新的组(1,2,3,4,5,'A','B',6,7,8) print "组成一个新的组(1,2,3,4,5,'A','B',6,7,8):"+str(tuple3[:5]+tuple4[0:2]+tuple3[5:]); #切片虽然可以重新组合拼接,但是截取一个元素的时候不能重新组合。因为截取一个元素时,会把它的类型看做是那个元素的类型而不是组合。 #print tuple4[2]+tuple3; #截取单个元素时,可以利用两个元素的范围来进行截取。 print "tuple4单个元素的截取和tuple3的组合拼接:"+str(tuple4[1:2]+tuple3); #解析:tuple4[2]取的值是字符串'B',tuple4[1:2]取的是元祖('B'),所以拼接时用tuple4[2]时会报错,tuple4[2:2]取的是空值 print "tuple4[2:2]:"+str(tuple4[2:2]); print "元祖和切片拼接的元祖不是同一个:"+str(tuple4)+"元祖id是:"+str(id(tuple4)); print "元祖和切片拼接的元祖不是同一个:"+str(tuple4[:2]+tuple4[2:])+"元祖id是:"+str(id(tuple4[:2]+tuple4[3:])); #元祖二级元素可以被修改 tuple5 = (1,2,3,'A',[4,5,6,['B',7,8],9],10,11); print "多级元素的元祖可以被修改:"+str(tuple5); #修改二级元素 tuple5[4][0] = 444; print "tuple[4] =444 :"+str(tuple5); tuple5[4][3][1]= 777; print "tuple5[4][3][1]= 777:"+str(tuple5);
运行结果:
末尾无逗号:1 末尾有逗号:(1,) 组成一个新的组(1,2,3,4,5,'A','B',6,7,8):(1, 2, 3, 4, 5, 'A', 'B', 6, 7, 8) tuple4单个元素的截取和tuple3的组合拼接:('B', 1, 2, 3, 4, 5, 6, 7, 8) tuple4[2:2]:() 元祖和切片拼接的元祖不是同一个:('A', 'B', 'C', 'D', 'E')元祖id是:52675592 元祖和切片拼接的元祖不是同一个:('A', 'B', 'C', 'D', 'E')元祖id是:53889208 多级元素的元祖可以被修改:(1, 2, 3, 'A', [4, 5, 6, ['B', 7, 8], 9], 10, 11) tuple[4] =444 :(1, 2, 3, 'A', [444, 5, 6, ['B', 7, 8], 9], 10, 11) tuple5[4][3][1]= 777:(1, 2, 3, 'A', [444, 5, 6, ['B', 777, 8], 9], 10, 11) Process finished with exit code 0

浙公网安备 33010602011771号