zoukankan      html  css  js  c++  java
  • 元组的常见操作

    '''
    元组:
    元组的创建
    元组的访问
    元组的遍历
    '''
    tuple=("apple","banana","grape","orange")
    print tuple[-1]
    print tuple[-2]
    #orange
    #grape
    tuple2=tuple[1:3]
    tuple3=tuple[0:-2]
    tuple4=tuple[2:-2]
    print tuple2
    print tuple3
    print tuple4
    #('banana', 'grape')
    #('apple', 'banana')
    #()

    fruit1=("apple","banana")
    fruit2=("grape","orange")
    tuple=(fruit1,fruit2)
    print tuple
    print "tuple[0][1]=",tuple[0][1]
    print "tuple[1][1]=",tuple[1][1]
    #(('apple', 'banana'), ('grape', 'orange'))
    #tuple[0][1]= banana
    #tuple[1][1]= orange


    #元组打包
    tuple=("apple","bananna","grape","orange")
    #解包
    a,b,c,d=tuple
    print a,b,c,d
    #>>>apple bananna grape orange

    #元组的只读性
    tuple = ("apple","banana","grape","orange")
    #tuple[0]="a"

    #特殊元组,当元组元素只有一个时,需要在最后面加一个逗号以区别圆括号和元组
    t=("apple",)
    print type(t)
    print tuple[-1]
    print tuple[-2]
    #越界报错
    #print tuple[-5]


    #使用range()遍历循环
    tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapeful",))
    for i in range(len(tuple)):
    print "tuple[%d]"%i,"",
    for j in range(len(tuple[i])):
    print tuple[i][j],"",
    print
    #使用map()循环遍历
    k=0
    for a in map(None,tuple):
    print "tuple[%d]:"%k,"",
    for x in a:
    print x,"",
    print
    k+=1
  • 相关阅读:
    C++ fstream 用法
    Servlet详解(转)
    JSP&Servlet(转)
    我们工作是为了什么!
    常见C C++问题(转)
    一份诚恳的互联网找工作总结和感想(附:怎样花两年时间去面试一个人)
    第一篇
    洛谷p1064 金明的预算方法
    onload、DOMContentLoaded与性能问题
    jsbin本地部署
  • 原文地址:https://www.cnblogs.com/papapython/p/7479319.html
Copyright © 2011-2022 走看看