本文实例讲述了Python3.4列表、数组操作。分享给大家供大家参考,具体如下:
python列表,数组类型要相同,python不需要指定数据类型,可以把各种类型打包进去
python列表可以包含整数,浮点数,字符串,对象
创建列表三种方式:
1.member = ["zdz","liufeng","hots"]
2.number = [1,2,3]
,
3.混合型 mix = [1,'zdz',3.12,[1,2,3]]
4.空列表 empty = []
向列表添加使用append
追加一个元素到末尾,extend
追加另外一个列表到末尾,insert
插入到指定位置
输出结果:
[]
[1, 'zdz', 3.12, [1, 2, 3]]
['zdz', 'liufeng', 'hots', 'good']
['zdz', 'liufeng', 'hots', 'good', 'cnblogs', 'google']
['alex', 'zdz', 'liufeng', 'hots', 'good', 'cnblogs', 'google']
['zdz', 'alex', 'liufeng', 'hots', 'good', 'cnblogs', 'google']
['zdz', 'alex', 'liufeng', 'hots', 'cnblogs', 'google']
['zdz', 'alex', 'hots', 'cnblogs', 'google']
['zdz', 'alex', 'hots', 'cnblogs']
['zdz', 'alex', 'cnblogs']
-----列表分片/切片 slice-----
['zdz', 'alex', 'cnblogs', 'hello']
['alex', 'cnblogs']
['zdz', 'alex', 'cnblogs', 'hello']
['zdz', 'alex', 'cnblogs']
['alex', 'cnblogs', 'hello']
['zdz', 'alex', 'cnblogs', 'hello']
-----列表常用操作符-----
False
False
True
True
False
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
[123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
4
[456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]
[4, 2, 5, 7, 9, 12, 35, 1]
[1, 2, 4, 5, 7, 9, 12, 35]
[1, 2, 4, 5, 7, 9, 12, 35]
[4, 2, 5, 7, 9, 12, 35, 1]
[35, 12, 9, 7, 5, 4, 2, 1]
[35, 12, 9, 7, 5, 4, 2, 1]
>>>
希望本文所述对大家Python程序设计有所帮助。
新闻热点
疑难解答