zoukankan      html  css  js  c++  java
  • Python数据结构:元组

    • 元组相加
    t1 = (1,2,3)
    t2 = (4,5,6)
    
    print(id(t1))
    print(id(t2))
    
    t1 += t2 #即t1=t1+t2,将t1与t2元组相加,t1指向相加之后的新元组,该操作合法,因为元素内容不能修改,但是可以指向新的地址
    
    print(t1)
    print(id(t1)) #相加之后的t1指向一个新的地址

     输出为

    2583036379208
    2583036379280
    (1, 2, 3, 4, 5, 6)
    2583035574600
    •  元组相乘
    t1 = (1,2,3)
    print(id(t1))
    t1 = t1 * 3 #运算之后生成新的元组,t1指向所生成的元素新地址
    print(t1) #输出(1, 2, 3, 1, 2, 3, 1, 2, 3)
    print(id(t1)) #t1指向了一个新的地址
    •  双层元组的遍历
    t = ((1,2,3),(6,7,8),("i","love","you"))
    for i in t:
        print(i)
    
    print("*"*20)
    
    for k,v,w in t:
        print(k,"...",v,"...",w)

     输出为

    (1, 2, 3)
    (6, 7, 8)
    ('i', 'love', 'you')
    ********************
    1 ... 2 ... 3
    6 ... 7 ... 8
    i ... love ... you
    •  两个变量交换
    a = (1,2,3)
    b = (7,8,9)
    print(a) #输出(1, 2, 3)
    print(b) #输出(7, 8, 9)
    print("*"*20)
    a,b =  b,a #两个元组交换,同样适用于其他各类型变量(比如int,list等等)
    print(a) #输出(7, 8, 9)
    print(b) #输出(1, 2, 3)
  • 相关阅读:
    微信支付退款部分代码
    Raspberry PI 点亮LED
    Raspberry PI 摄像头
    RaspberryPI 3B系统烧录
    MySQL基础使用
    MySQL数据库管理系统概述
    Eclipse创建JAVA项目
    Brup Suite拦截https请求
    Github 第一个仓库
    python os模块主要函数
  • 原文地址:https://www.cnblogs.com/mryanzhao/p/9511404.html
Copyright © 2011-2022 走看看