• 012 元组


    简介

    • 英文名:tuple
    • 可以看成不可更改的 list

    创建

    • 提示:逗号比括号要关键!

    例1

    >>> t1 = ()
    >>> t2 = (1,)
    >>> t3 = 1,
    >>> t4 = (1)
    >>> 
    >>> type(t1)
    <class 'tuple'>
    >>> type(t2)
    <class 'tuple'>
    >>> type(t3)
    <class 'tuple'>
    >>> type(t4)
    <class 'int'>
    >>> 
    

    例2

    >>> t1 = (1, 2, 3)
    >>> t2 = ([1, 2, 3])
    >>> t3 = tuple([1, 2, 3])
    >>> 
    >>> type(t1)
    <class 'tuple'>
    >>> type(t2)
    <class 'list'>
    >>> type(t3)
    <class 'tuple'>
    >>> 
    

    例3

    >>> t = tuple(1)
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        t = tuple(1)
    TypeError: 'int' object is not iterable
    >>> 
    

    特性

    概述

    • 元组是序列表,有序
    • 元组数据值相当于“只读”模式,即,可以访问但不能修改
    • 元组数据值可以是任意类型
    • 与 list 仅一个区别:不可修改
    • 可以索引、分片、序列间计算、成员资格操作等

    索引

    >>> t = (1, 2, 3)
    >>> t[0]
    1
    >>> t[-1]
    3
    >>> t[5]
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        t[5]
    IndexError: tuple index out of range
    >>> 
    

    分片

    >>> t1 = (1, 2, 3, 4, 5)
    >>> t2 = t1[::2]
    >>> t1
    (1, 2, 3, 4, 5)
    >>> t2
    (1, 3, 5)
    >>> 
    >>> t1[3:10]
    (4, 5)
    >>> 
    

    序列运算

    >>> t1 = (1, 2, 3)
    >>> t2 = (4, 5, 6)
    >>> id(t1)
    2284419977472
    >>> t1 = t1 + t2
    >>> t1
    (1, 2, 3, 4, 5, 6)
    >>> id(t1)
    2284420072640
    >>> 
    >>> t2 * 3
    (4, 5, 6, 4, 5, 6, 4, 5, 6)
    >>> 
    

    成员检测

    >>> t = (1, 2, 3)
    >>> 1 in t
    True
    >>> 
    

    遍历

    例1

    >>> t1 = (1, 2, 3, "york", "fish")
    >>> for i in t1:
        print(i, end=' ')
    
        
    1 2 3 york fish 
    >>> 
    >>> t2 = ((1, 2, 3), (4, 5, 6), ("york", "fish"))
    >>> for i in t2:
        print(i, end=' ')
    
        
    (1, 2, 3) (4, 5, 6) ('york', 'fish') 
    >>> 
    

    例2

    >>> t = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
    >>> for i, j, k in t:
        print(i, j, k)
    
        
    1 2 3
    4 5 6
    7 8 9
    >>> 
    

    内置方法

    查看

    • dir(tuple): 列出 tuple 的方法
    • help(tuple): 查看开发者对 tuple 方法所编写的详细描述文档
      • help(tuple.count) 可以仅查看 count() 的用法

    count()

    • 与字符串、列表中的用法类似
    • 计算指定数据在元组中出现的次数
    >>> t = (1, 2, 3, 3, 3, 4, 5, 6)
    >>> t.count(3)
    3
    >>> 
    

    index()

    • 与字符串、列表中的用法类似
    • 索引指定元素在元组中的位置
    >>> t = (1, 2, 3, 3, 3, 4, 5, 6)
    >>> t.index(3)
    2
    >>> t.index(3, 4)
    4
    >>> t.index(10)
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        t.index(10)
    ValueError: tuple.index(x): x not in tuple
    >>> 
    

    别的方法

    len()

    >>> t = (1, 2, 3)
    >>> len(t)
    3
    >>> 
    

    max() & min()

    >>> t = (1, 3, 5, 6, 4, 2)
    >>> max(t)
    6
    >>> min(t)
    1
    >>> 
    

    del

    >>> t = (1, 2, 3)
    >>> del t
    >>> t
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        t
    NameError: name 't' is not defined
    >>> 
    
    • 写成 del(t) 也行

    注意事项

    例1 “第二层”

    >>> t = (1, [2, 3, 4], 5)
    >>> t[1][0] = 22
    >>> t
    (1, [22, 3, 4], 5)
    >>> 
    

    例2

    >>> t1 = ("Amy Green", "Johnny Lee", "Tony black")
    >>> t1 = t1[:2] + ("York Fish",) + t1[2:]  # t[2:] 不能偷懒写成 t[-1]
    >>> t1
    ('Amy Green', 'Johnny Lee', 'York Fish', 'Tony black')
    >>> 
    >>> t2 = ("Amy Green", "Johnny Lee", "Tony black")
    >>> t2 = t2[:2] + ("York Fish",) + t2[-1]
    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in <module>
        t2 = t2[:2] + ("York Fish",) + t2[-1]
    TypeError: can only concatenate tuple (not "str") to tuple
    >>> 
    

    例3

    >>> t = ('Amy Green', 'Johnny Lee', 'York Fish', 'Tony black')
    >>> t = t[:2] + t[3:]
    >>> t
    ('Amy Green', 'Johnny Lee', 'Tony black')
    >>> 
    
  • 相关阅读:
    【HDOJ1534】【差分约束+SPFA】
    【HDOJ3861】【Tarjan缩点+最小路径覆盖】
    【二分图最大权完美匹配】【KM算法】【转】
    学习一点汇编 INT 16H指令
    POJ2449 Remmarguts' Date 第K短路
    POJ3090 巧用欧拉函数 phi(x)
    POJ3420 递推+矩阵快速幂
    UVALive 4671 K-neighbor substrings 巧用FFT
    Tyvj 1068 巧用扩展KMP
    第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
  • 原文地址:https://www.cnblogs.com/yorkyu/p/10279814.html
Copyright © 2020-2023  润新知