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

    元组是一种序列,它与列表一样,唯一不同的是列表是可修改的,而元组不可以,元组的创建也是较为简单的。

    1、元组创建

    • 直接使用逗号、圆括号
    t=(1,2,3)
    print(type(t))#<class 'tuple'>
    • 使用关键字tuple
    class tuple(object):
        """
        tuple() -> empty tuple
        tuple(iterable) -> tuple initialized from iterable's items
        
        If the argument is a tuple, the return value is the same object.
        """
        def count(self, value): # real signature unknown; restored from __doc__
            """ T.count(value) -> integer -- return number of occurrences of value """
            return 0
    
        def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
            """
            T.index(value, [start, [stop]]) -> integer -- return first index of value.
            Raises ValueError if the value is not present.
            """
            return 0
    
        def __add__(self, *args, **kwargs): # real signature unknown
            """ Return self+value. """
            pass
    
        def __contains__(self, *args, **kwargs): # real signature unknown
            """ Return key in self. """
            pass
    
        def __eq__(self, *args, **kwargs): # real signature unknown
            """ Return self==value. """
            pass
    
        def __getattribute__(self, *args, **kwargs): # real signature unknown
            """ Return getattr(self, name). """
            pass
    
        def __getitem__(self, *args, **kwargs): # real signature unknown
            """ Return self[key]. """
            pass
    
        def __getnewargs__(self, *args, **kwargs): # real signature unknown
            pass
    
        def __ge__(self, *args, **kwargs): # real signature unknown
            """ Return self>=value. """
            pass
    
        def __gt__(self, *args, **kwargs): # real signature unknown
            """ Return self>value. """
            pass
    
        def __hash__(self, *args, **kwargs): # real signature unknown
            """ Return hash(self). """
            pass
    
        def __init__(self, seq=()): # known special case of tuple.__init__
            """
            tuple() -> empty tuple
            tuple(iterable) -> tuple initialized from iterable's items
            
            If the argument is a tuple, the return value is the same object.
            # (copied from class doc)
            """
            pass
    
        def __iter__(self, *args, **kwargs): # real signature unknown
            """ Implement iter(self). """
            pass
    
        def __len__(self, *args, **kwargs): # real signature unknown
            """ Return len(self). """
            pass
    
        def __le__(self, *args, **kwargs): # real signature unknown
            """ Return self<=value. """
            pass
    
        def __lt__(self, *args, **kwargs): # real signature unknown
            """ Return self<value. """
            pass
    
        def __mul__(self, *args, **kwargs): # real signature unknown
            """ Return self*value.n """
            pass
    
        @staticmethod # known case of __new__
        def __new__(*args, **kwargs): # real signature unknown
            """ Create and return a new object.  See help(type) for accurate signature. """
            pass
    
        def __ne__(self, *args, **kwargs): # real signature unknown
            """ Return self!=value. """
            pass
    
        def __repr__(self, *args, **kwargs): # real signature unknown
            """ Return repr(self). """
            pass
    
        def __rmul__(self, *args, **kwargs): # real signature unknown
            """ Return self*value. """
            pass
    tuple类
    t=tuple([1,2,3])
    print(type(t))#<class 'tuple'>

    注意:如果创建一个元素的元组,后面需要加逗号。

    2、元组操作

    之前说过元组是一种序列,那么序列的所有特性元组都是可以使用的。那么序列有哪些特性呢?

    • 索引取值
    • 分片
    • 序列相加
    • 序列乘法
    • in运算符
    • 序列内建函数
    #索引、切片取值
    t=(1,2,3,4,5,)
    print(t[1:3])#(2, 3)
    
    #元组相加
    t=(1,2,3,4,5,)
    t1=('hello',)
    print(t+t1)#(1, 2, 3, 4, 5, 'hello')
    
    #元组乘法
    t=(1,2,3,4,5,)
    print(t*2)#(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
    
    #in运算符
    t=(1,2,3,4,5,)
    print(3 in t)#True
    
    #内建函数
    t=(1,2,3,4,5,)
    print(len(t))#5
    print(min(t))#1
    print(max(t))#5
  • 相关阅读:
    bootstrap 导航栏、输入框按钮组、栅格系统
    Python排序算法冒泡排序选择排序插入排序
    Python 解螺旋数组
    初到博客园,请多指教
    gcd, map for ocaml
    qsort for ocaml
    子序列和最大的问题
    将一个整数划分为非负整数的部分
    filter in Ocaml
    scheme中表只能操作头部带来的一个问题
  • 原文地址:https://www.cnblogs.com/shenjianping/p/11002139.html
Copyright © 2011-2022 走看看