python元组Tuple的遍历

现提供两种方法对元组进行遍历

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

转自 http://blog.163.com/fy_ay/blog/static/22028347200971985556438/

你可能感兴趣的:(Python)