现提供两种方法对元组进行遍历
1)range()
#!/usr/bin/python
#-*- coding:UTF-8 -*-
tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))
for i in range(len(tuple)) :
for j in range(len(tuple[i])):
#print("tuple[",i,"][",j,"]=",end=""),#一种显示方法
#print("tuple[%d]"%i,"[%d]="%j,end=""),#另一种显示方法
print("tuple[%d][%d]"%(i,j))
print(tuple[i][j],"")
print
2)map()遍历
#!/usr/bin/python
#-*- coding:UTF-8 -*-
#import os
tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))
def ret (x):
return x
k=0
for a in map(ret,tuple):
j=0
for x in a:
print("tuple[%d][%d]="%(k,j),end=""),
print(x," "),
j+=1
print
k+=1