品易云推流 关闭
文章详情页
文章 > Python基础教程 > python列表的优点探究

python列表的优点探究

头像

小妮浅浅

2021-05-14 09:42:574176浏览 · 0收藏 · 0评论

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电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图