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

    '''元组的方法
    t.index()
    t.count()
    '''
    
    # 元组由于不可更改元组里面的数据(第一层)
    # 所以元组可操作的方法比较少
    
    t = (1,)  # 单个元素,为了区分,加个逗号才叫元组
    t1 = (3, 4, 'hello', [2, 3, 'ppp'], 3)
    print(t1[2])
    n1 = t1.count(3)     # count(元组里面的元素)同之前的用法,返回参数这个数据在元组中出现的次数
    print(n1)            # 没有的话,则返回0
    n2 = t1.index(3)   # index(元组里的元素)从左往右索引,找到了返回下标,没找到则报错
    print(n2)
    t2 = (('a', 'A'), (2, 2), ('c', 'C'))
    for i in t2:
        print(i)
    
    for i, j in t2:   # 要注意一点,拆包过程中组合可以是[( , ), ( , )]或(( , ),( , ))
        print(i)                              # [[ , ],[ , ]]或([ , ],[ , ])
        print(j)
    

      

                                                                       -------  知识无价,汗水有情,如需搬运请注明出处,谢谢!

  • 相关阅读:
    【371】Twitter 分类相关
    【370】Python列表生成式(for 写入一行)
    Netbeans 中的编译器相关配置
    SP*
    du 命令
    闲杂
    Shell重定向&>file、2>&1、1>&2的区别
    Shell编程中Shift的用法
    shell中一维数组值得获取
    shell expr的用法
  • 原文地址:https://www.cnblogs.com/wf-skylark/p/9009733.html
Copyright © 2011-2022 走看看