美文网首页
Python 数据结构 tuple

Python 数据结构 tuple

作者: __RY__ | 来源:发表于2018-08-17 11:08 被阅读2次

    元组

    • 一个有序的元素组成的集合
    • 使用小括号()表示
    • 元组是不可变对象

    定义

    • tuple() -> empty tuple
    • tuple(iterable) -> tuple initialized from iterable's items
    • t=(1,) # 一个元组的定义,注意有个逗号

    时间复杂度

    • index和count方法都是O(n)
    • 随着列表数据规模的增大,而效率降低

    元组是只读的,所以增、删、改方法都没有


    命名元组 namedtuple

    namedtuple (typename, field_names, *, verbose=False, rename=False, module=None)

    • 命名元组,返回一个元组的子类,并定义了字段
    • field_names 可以是空白符或逗号分割的字符串,可以是字段的列表
    >>> from collections import namedtuple
    >>> Point = namedtuple('_Point',['x','y'])
    >>> P = Point(11,22)
    >>> Student = namedtuple('Student','name age')
    >>> Tom = Student('tom',20)
    >>> Jerry = Student('jerry',18)
    >>> tom.name
    'tom'
    >>>
    >>> jerry.age
    18
    >>>
    

    相关文章

      网友评论

          本文标题:Python 数据结构 tuple

          本文链接:https://www.haomeiwen.com/subject/onszbftx.html