python中二元元组的使用

# ! /usr/bin/python
#  _* _coding:UTF-8 _*_

#  the use of 2-tuple
fruit1 = ( " apple " , " banana " )
fruit2 = ( " grape " , " orange " )
tuple1 = (fruit1,fruit2)
print  tuple1
print  tuple1[0][ 1 ]
print  tuple1[ 1 ][ 1 ]

print   " ################################################################################ "

#  the package and unpackage

tuple3 = ( " apple " , " banana " , " grape " , " orange " )
a,b,c,d = tuple3
print  a,b,c,d

print   " ################################################################################ "

# traverse the tuple
tuple4 = (( " apple " , " banana " ),( " grape " , " orange " ),( " watermelon " ,),( " grapefulit " ,))
for  i  in  range(len(tuple4)):
    
print   " tuple[%d]: "   % i, "" ,
    
for  j  in  range(len(tuple4[i])):
        
print  tuple4[i][j], "" ,
    
print

print   " ################################################################################ "

#  map()
tuple5 = (( " apple " , " banana " ),( " grape " , " orange " ),( " watermelon " ,),( " grapefulit " ,))
k = 0
for  a  in  map(None,tuple5) :
    
print   " tuple[%d]: "   % k, "" ,
    
for  x  in  a :
        
print  x, "" ,
    
print
    k += 1
    

 

你可能感兴趣的:(python)