1、Python的列表与其他语言中的数组有些类似,但是列表要强大得多。其中一个方面就是,列表没有固定类型的约束。例如,下面例子中接触到的列表,包含了三个完全不同类型的对象(一个整数、一个字符串,以及一个浮点数)。
>>>L[0]#Indexingbyposition 123 >>>L[:-1]#Slicingalistreturnsanewlist [123,'spam'] >>>L+[4,5,6]#Concatenationmakesanewlisttoo [123,'spam',1.23,4,5,6] >>>L#We'renotchangingtheoriginallist [123,'spam',1.23] 123456789101112131415161718192021
2、列表没有固定大小,也就是说能够按照需要增加或减小列表大小,来响应其特定的操作。
>>>L.append('NI')#Growing:addobjectatendoflist >>>L [123,'spam',1.23,'NI'] >>>L.pop(2)#Shrinking:deleteaniteminthemiddle 1.23 >>>L#"delL[2]"deletesfromalist [123,'spam','NI'] 12345678910111213
以上就是python列表的优点探究,希望能对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。