python--元組tuple

xie仗劍天涯發表於2017-03-03

元組與列表一樣,都是序列。但元組不能修改內容(列表允許)

預設的,元組通過圓括號括起來

1. 使用type函式檢視型別

numbers = (1,2,3,4,5,6,7,8,9,0)
print(type(numbers))

2. tuple 函式

tuple 函式的功能與list函式基本一樣:以一個序列作為引數並把它轉換為元組。

AAA = [1,2,3,4,5]
print (type(AAA))
BBB = tuple(AAA)
print (type(BBB))

元組的基本操作

同其他序列(如:索引,分片,相加,相乘)

1.索引
CCC = (11,22,33,44,55,66,77,88)
print (CCC[0])
print (CCC[4])
print (CCC[-1])

注:使用負數索引,python會從右邊開始計算。最後一個元素的位置編號是-1

2.分片
DDDD = (12,222,112,333,44,1234,11111,1,33,455,66667,87787)
print (DDDD[3:6]) # 獲取第4個到第6個元素
print (DDDD[-3:]) # 獲取最好三個元素
print (DDDD[:3])  # 獲取前三個元素

print (DDDD[0:10:2]) # 步長為2分片
print (DDDD[::4])    # 步長為4分片
print (DDDD[::-1])   # 從右到左提取元素
3.序列相加
EE = (2,3,4,5,6)
FF = (11,33,32,88,90)
print (EE+FF)
4.序列相乘
GG = ("I","LOVE","PYTHON","~")
print (GG*5)
HH = (12,23,34,56,78)
print (HH*5)

注:元組的內容不能修改

5.統計元素出現次數count函式
HH = (12,22,33,33,44,44,44,55,12)
print (HH.count(12))
print (HH.count(44))

元組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

相關文章