zoukankan      html  css  js  c++  java
  • 四,元组类型

     什么是元组: 元组就是一个不可变的列表
     ======================================基本使用======================================
     1、用途: 用于存放多个值,当存放的多个值只有读的需求没有改的需求时用元组最合适

     2、定义方式:在()内用逗号分隔开多个任意类型的值
    t=(1,3.1,'aaa',(1,2,3),['a','b']) # t=tuple(...)
     print(type(t))

     res=tuple('hello')
     res=tuple({'x':1,'y':2})
     print(res)

     3、常用操作+内置的方法
    优先掌握的操作:
    1、按索引取值(正向取+反向取):只能取
     t=('a','b',1)
     t[0]=111#不支持修改,赋值

    2、切片(顾头不顾尾,步长)
     t=('h','e','l','l','o')
     res=t[1:3]
     print(res)
     print(t)

    3、长度
     t=('h','e','l','l','o')
     print(len(t))

    4、成员运算in和not in
     t=('h','e','l','l','o')
     print('h' in t)

    5、循环
     t=('h','e','l','l','o')
     for item in t:
     print(item)


     ======================================该类型总结====================================
     存多个值

     有序

     不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
    t=(1,'a',['x','y','z'])
     print(id(t[2]))

     print(id(t))
    t[2][0]='X'
     print(t)
     print(id(t))

     print(id(t[2]))


     list1=['a','b','c']
     print(id(list1[0]))
     print(id(list1[1]))
     print(id(list1[2]))

     print('='*50)
     list1[1]='B'
     print(id(list1[0]))
     print(id(list1[1]))
     print(id(list1[2]))

     掌握的方法
    t=('a','b','a')
     print(t.index('a'))
     t.index('xxx')
    print(t.count('a'))

  • 相关阅读:
    hdu1087Super Jumping! Jumping! Jumping!
    hdu1159Common Subsequence(最长公共子序列)
    hdu1069Monkey and Banana(最长递增子序列)
    poj2533(最长递增子序列)
    hdu1029Ignatius and the Princess IV
    uva10622(唯一分解定理)
    myeclipse设置技巧
    myeclipse2014新感悟
    小错误汇总
    字符串反转
  • 原文地址:https://www.cnblogs.com/martin-wang/p/10030204.html
Copyright © 2011-2022 走看看