python列表的优点探究

325次阅读
没有评论

python列表的优点探究

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列表的优点探究,希望能对大家有所帮助。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-02发表,共计773字。
新手QQ群:570568346,欢迎进群讨论 Python51学习