騰訊課堂——基礎數據類型(tuple元祖)

初識元祖

標準類型
數字
字符串
列表
字典
元組

 

 

 

 

 

 

 

   定義:與列表相似,只不過[]改爲()ide

  特性:
函數

    1.可存放多個值
    2.不可變
    3.
按照從左到右的順序定義元組元素,下標從0開始順序訪問,有序post

元祖的建立

  ages = (11, 22, 33, 44, 55)
  或
  ages = tuple((11, 22, 33, 44, 55))
  順序及下標圖示

元祖的經常使用操做

  索引spa

  切片  rest

  循環code

  長度blog

  包含索引

元祖的特性詳解

  1.可存放多個值ci

    若是元祖中只有一個值字符串

t = (1,)
t = (1)   #<==>t = 1

    元祖中不只能夠存放數字、字符串,還能夠存放更加複雜的數據類型

  2.不可變 

    元祖自己不可變,若是元祖中還包含其餘可變元素,這些可變元素能夠改變

元祖相關知識拾遺

  字典中遇到的tuple

   1.字典的定義

    元祖能夠做爲字典的key:

dic = {('alex','male'):'v1',('wusir','male'):'v2'}
print(dic)

     字典定義能夠用到元祖:

person = dict((['name','苑昊'],['文周',18]))
person = dict((('name','苑昊'),('文周',18)))
print(person)

   2.字典的items方法,將字典中的每一項做爲元祖返回

dic = {'k1':'v1','k2':'v2'}
print(dic.items())
#dict_items([('k1', 'v1'), ('k2', 'v2')])

    再講循環

dic = {'key1':'value1','key2':'value2'}
for key,value in dic.items():
    print(key,value)

 

元祖的工廠函數

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
元祖的工廠函數
相關文章
相關標籤/搜索