python基本數據類型——tuple


一、元組的創建與轉換:

ages = (11, 22, 33, 44, 55)
ages = tuple((11, 22, 33, 44, 55))
ages = tuple([]) # 字符串、列表、字典(默認是key)
  • 元組基本上可以看成不可修改的列表
  • tuple(iterable),可以存放所有可迭代的數據類型

 

二、元組的不可變性

如:t = (17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse'])
元組t中的元素數字17和字符串‘Jesse’以及元組('LinuxKernel', 'Python')本身屬於不可變元素,故其在元組中不可更新;但是其中包含的列表[17, 'Jesse']本身屬於可變元素,故:

>>> t = (17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse'])
>>> t
(17, 'Jesse', ('LinuxKernel', 'Python'), [17, 'Jesse'])
>>> t[0] = 18
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t[3]
[17, 'Jesse']
>>> t[3][0] = 21
>>> t
(17, 'Jesse', ('LinuxKernel', 'Python'), [21, 'Jesse'])

 

三、元組常用操作

#count(self,value)
#功能:統計當前元組中某元素的個數
tup = (55,77,85,55,96,99,22,55,)
tup.count(55)
#返回結果:3   元素‘55’在元組tup中出現了3次



#index(self, value, start=None, stop=None)
功能:獲取元素在元組中的索引值,對於重復的元素,默認獲取從左起第一個元素的索引值
tup = (55,77,85,55,96,99,22,55,)
tup.index(55)
返回結果:0
tup.index(85)
返回結果:2
tup.index(55,2,7)
返回結果:3
lass 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, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self): # real signature unknown; restored from __doc__
        """ x.__hash__() <==> hash(x) """
        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): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ T.__sizeof__() -- size of T in memory, in bytes """
        pass

tuple
tuple源碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM