Python 元组

Tuple 数据类型

一、如何创建元组

>>> t = (1, 2, 3, 'root')
>>> t
(1, 2, 3, 'root') 

二、元组和列表的区别

  • list是一种有序的集合,可以随时添加和删除其中的元素。
  • 元组也是一种有序列表,和list非常类似,不同点是tuple一旦定义了就不可修改,在一定意义上这也提高了代码的安全性,查询方法和list一样
>>> dir(a)
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> dir(t)
['count', 'index'] 

>>> type(a)
<class 'list'>
>>> type(t)
<class 'tuple'>
tuple 函数方法
class tuple(object):
    """
    Built-in immutable sequence.
    
    If no argument is given, the constructor returns an empty tuple.
    If iterable is specified the tuple is initialized from iterable's items.
    
    If the argument is a tuple, the return value is the same object.
    """
    def count(self, *args, **kwargs): # real signature unknown
        """ Return number of occurrences of value. """
        pass

    def index(self, *args, **kwargs): # real signature unknown
        """
        Return first index of value.
        
        Raises ValueError if the value is not present.
        """
        pass

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __class_getitem__(self, *args, **kwargs): # real signature unknown
        """ See PEP 585 """
        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__
        """
        Built-in immutable sequence.
        
        If no argument is given, the constructor returns an empty tuple.
        If iterable is specified the tuple is 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. """
        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 value*self. """
        pass

三、元组如何转换为列表

>>> list(t)
[1, 2, 3, 'root'] 

四、可变的tuple

# 当元祖内部放一个列表的时候,这个元祖中的列表的值就可以变化了,实际上tuple并没有改变,变的是内部list的值
>>> m = (1, 'A', ['root', 'gm'])
>>> m
(1, 'A', ['root', 'gm'])

# 查看元组第3个元素的值
>>> m[2]
['root', 'gm']

# 其查询列表和元组的数据结构类似与C语言中的1维数组和2维数组
>>> m[2][1]
'gm'

# 修改原列表第2个元素的值
>>> m[2][1]='hlr'

# 查看元组m
>>> m
(1, 'A', ['root', 'hlr'])
posted @ 2023-06-09 17:31  evescn  阅读(16)  评论(0编辑  收藏  举报