• 技术文章 >Python技术 >Python基础教程

    python列表的优点探究

    小妮浅浅小妮浅浅2021-05-14 09:42:57原创4016

    1、Python的列表与其他语言中的数组有些类似,但是列表要强大得多。其中一个方面就是,列表没有固定类型的约束。例如,下面例子中接触到的列表,包含了三个完全不同类型的对象(一个整数、一个字符串,以及一个浮点数)。

    >>> L[0]                 # Indexing by position
     
    123
     
     
     
    >>> L[:-1]               # Slicing a list returns a new list
     
    [123,'spam']
     
     
     
    >>> L + [4,5,6]        # Concatenation makes a new list too
     
    [123,'spam',1.23,4,5,6]
     
     
     
    >>> L                    # We're not changing the original list
     
    [123,'spam',1.23]
    123456789101112131415161718192021

    2、列表没有固定大小,也就是说能够按照需要增加或减小列表大小,来响应其特定的操作。

    >>> L.append('NI')           # Growing: add object at end of list
     
    >>> L
     
    [123,'spam',1.23,'NI']
     
    >>> L.pop(2)                 # Shrinking: delete an item in the middle
     
    1.23
     
    >>> L                        # "del L[2]" deletes from a list
     
    [123,'spam','NI']
    12345678910111213

    以上就是python列表的优点探究,希望能对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python列表
    品易云
    上一篇:python模块的搜索路径和顺序 下一篇:python中time模块的时间格式

    相关文章推荐

    • python进程的交流方式• python中Queue和pipe的差别• python中re有哪些常用函数• python堆排序是什么?• python创建和使用堆的方法• python中super的使用注意• python如何在自定义类上使用堆排序• python中的mro是什么?• python模块的搜索路径和顺序

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网