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
  • 相关阅读:
    NOsql总结
    关于Swift中的指针的那些事
    并发控制的概念
    并发控制--Concurrency control--乐观、悲观及方法
    数据库的三大系统
    数据库沉思录
    代码结构化(分层)阅读
    代码阅读困难的原因
    数据库锁与并发
    SQLite事务、错误与自动回滚
  • 原文地址:https://www.cnblogs.com/papapython/p/7479319.html
Copyright © 2011-2022 走看看