zoukankan      html  css  js  c++  java
  • 元组内置函数用法

    元组内置函数用法

    元组tuple,我们可以把它看做是 只读列表

    • 列表转换为元组: tuple=tuple(list) 例如: list=['Wjz','wjk','wz','hacker','beijing'] tuple=tuple(list) print(tuple) # 输出 ('Wjz','wjk','wz','hacker','beijing')
    • 字典转换为元组,元组中元素为字典的key: tuple=tuple(dict) 例如: dic={'wjz':1,'wjk':2} tuple=tuple(dic) print(tuple) # 输出 ('wjz', 'wjk')
    • 字符串转换为元组,字符串的每一个字符都为元组的元素: tuple=tuple(str) 例如: str='wjz wjk' tuple=tuple(str) print(tuple) # 输出 ('w', 'j', 'z', ' ', 'w', 'j', 'k')

    1.定义:在()内可以有多个任意类型的值,用逗号分隔元素,用于表示 多个东西

    2.作用:类似于列表,不过只能取不能更改

    name_list = ['qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf']
    my_friend=tuple(name_list)  #把列表转化为元组
    print(f'my_friend:{my_friend}')
    my_best_friend = ('kang yuan') #默认定义元素为字符串型
    my_best_friend_tuple = ('jia','kang','yuan') #定义元素类型为元组
    print(f'type(my_best_friend):{type(my_best_friend)}')
    print(f'type(my_best_feiend_tuple):{type(my_best_friend_tuple)}')
    
    my_friend:('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf')
    type(my_best_friend):<class 'str'>
    type(my_best_feiend_tuple):<class 'tuple'>
    

    3.常用操作+内置方法:常用操作和内置方法:

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

    1.索引取值

    name_tuple = ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    name_tuple[3]
    print(f"name_tuple[3]:{name_tuple[3]}")
    
    name_tuple[3]:mkou
    

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

    name_tuple = ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    print(f"name_tuple[2:8:2]:{name_tuple[2:8:2]}") #取出索引值2-7,步长为2的元素
    print(f'name_tuple[0:5]:{name_tuple[0:5]}') #取出索引值0-4,步长为1的元素
    print(f'name_tuple[0:]:{name_tuple[0:]}')  #取出索引值0及之后,步长为1的元素
    print(f"name_tuple[:8]:{name_tuple[:8]}")  #取出索引值0-7,步长为1的元素
    print(f"name_tuple[:]:{name_tuple[:]}")   #取出所有的步长为1的元素
    print(f"name_tuple[::2]:{name_tuple[::2]}")  #取出所有步长为2的元素
    print(f"name_tuple[::-1]:{name_tuple[::-1]}")  #取出所有步长为 -1的元素
    
    name_tuple[2:8:2]:('mkou', 'bdgyh', 'mkdfia')
    name_tuple[0:5]:('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh')
    name_tuple[0:]:('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf')
    name_tuple[:8]:('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf')
    name_tuple[:]:('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf')
    name_tuple[::2]:('qwesc', 'mkou', 'bdgyh', 'mkdfia')
    name_tuple[::-1]:('nmjigf', 'mkdfia', 'mkihgg', 'bdgyh', 'mkou', 'mkou', 'zxcsd', 'qwesc')
    

    3.长度len(计算元组中元素总的个数)

    name_tuple ,name_tuple2 = ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf') ,('mhdsjh','jhhbxnhd')
    print(f'first_length:{len(name_tuple)}')
    print(f"second_lenght:{len(name_tuple2)}")
    
    first_length:8
    second_lenght:2
    

    4.成员计算in 和not in

    name_tuple= ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    print(f"'band' not in name_tuple:{'band' not in name_tuple}")
    print(f"'band' in name_tuple:{'band' in name_tuple}")
    
    'band' not in name_tuple:True
    'band' in name_tuple:False
    

    5.循环(for循环)

    name_tuple = ('qwesc', 'zxcsd', 'mkou', 'mkou', 'bdgyh', 'mkihgg', 'mkdfia', 'nmjigf')
    for mottery in range(len(name_tuple)):
        print(mottery,name_tuple[mottery])
    
    0 qwesc
    1 zxcsd
    2 mkou
    3 mkou
    4 bdgyh
    5 mkihgg
    6 mkdfia
    7 nmjigf    
    

    6.count(计算元组中相同元素的个数)

    name_tuple= ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    print(f"name_tuple.count('mkou'):{name_tuple.count('mkou')}")  # 获取指定元素的个数
    
    name_tuple.count('mkou'):2
    

    7.index(索引,用于指出元组中某一元素的索引)

    name_tuple= ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    print(f"first:{name_tuple.index('mkou')}")      # 获取指定元素的索引值
    print(f"second:{name_tuple.index('bdgyh')}")
    print(f"threid:{name_tuple.index('mkdfia')}")
    
    first:2
    second:4
    threid:6
    

    4.存一个值or多个值:多个值

    5.有序or无序:有序(可以按照索引取值)

    name_tuple= ('qwesc','zxcsd','mkou','mkou','bdgyh','mkihgg','mkdfia','nmjigf')
    print(f"id:{id(name_tuple)}")
    
    

    id:6439920

    6.可变or不可变:无可变与不可变数据类型一说

    Never,Never,Never give up.
  • 相关阅读:
    Project2016下载安装密钥激活教程破解
    关于 Level 和 Promotion,其实就那么简单
    Docker 入门
    Spring Cloud Config中文文档
    如何合理设置线程池大小
    java中线程池的使用
    快速掌握和使用Flyway
    duilib教程之duilib入门简明教程6.XML配置界面
    duilib教程之duilib入门简明教程5.自绘标题栏
    duilib教程之duilib入门简明教程4.响应按钮事件
  • 原文地址:https://www.cnblogs.com/zuihoudebieli/p/10928809.html
Copyright © 2011-2022 走看看