zoukankan      html  css  js  c++  java
  • 5.c元组类型内置方法

    元组类型内置方法

    元组是不可变的列表,即元组的值不可更改,因此元组一般只用于只存不取的需求。也因此元组可以被列表取代掉,所以元组相比较列表使用的很少。元组相比较列表的优点为:列表的值修改后,列表的结构将会发生改变,而元组只需要存储,因此列表在某种程度上而言需要占用更多的内存。

    优先掌握(*****)

    1. 索引取值
    2. 切片(顾头不顾尾,步长)
    3. 长度len
    4. 成员运算in和not in
    5. 循环
    6. count
    7. index

    1.索引取值

    # tuple之索引取值
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    # name_tuple[0] = 'nick handsom'  # 报错
    
    print(f"name_tuple[0]: {name_tuple[0]}")
    name_tuple[0]: nick
    

    2.切片(顾头不顾尾,步长)

    # tuple之切片
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")
    name_tuple[1:3:2]: ('jason',)
    

    3.长度len

    # tuple之长度
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    print(f"name_tuple: {name_tuple}")
    name_tuple: ('nick', 'jason', 'tank', 'sean')
    

    4.成员运算in和not in

    # tuple之成员运算
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    print(f"'nick' in name_tuple: {'nick' in name_tuple}")
    'nick' in name_tuple: True
    

    5.循环

    # tuple之循环
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    for name in name_tuple:
        print(name)
    nick
    jason
    tank
    sean
    

    6.count()

    # tuple之count()
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    print(f"name_tuple.count('nick'): {name_tuple.count('nick')}")
    name_tuple.count('nick'): 1
    

    7.index()

    # tuple之index()
    name_tuple = ('nick', 'jason', 'tank', 'sean')
    
    print(f"name_tuple.index('nick'): {name_tuple.index('nick')}")
    name_tuple.index('nick'): 0
    
  • 相关阅读:
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)(A-C题解)
    最小生成树
    并查集
    字符串的匹配算法
    CTU Open Contest 2019 AB题
    Codeforces Round #624 (Div. 3)(题解)
    Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
    Lyft Level 5 Challenge 2018
    终于在博客写完第一篇题解啦
    Codeforces Round #519 by Botan Investments(前五题题解)
  • 原文地址:https://www.cnblogs.com/yellowcloud/p/10832095.html
Copyright © 2011-2022 走看看