本節內容:python
本節先介紹列表。app
help()是ipython裏特有的函數,能夠打印出類、函數的一些信息。函數
In [1]: help(list) Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(self, value, /) | Return self>value. | | __iadd__(self, value, /) | Implement self+=value. | ...
1. 初始化列表ui
In [2]: lst = list() In [3]: lst = [] In [4]: lst Out[4]: [] In [5]: lst = [1, 2, 3] In [6]: lst Out[6]: [1, 2, 3]
2. 下標/索引操做lua
In [6]: lst Out[6]: [1, 2, 3] In [7]: lst[0] Out[7]: 1 In [8]: lst[-1] Out[8]: 3
In [9]: lst[-4] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-9-7ea420056b9a> in <module>() ----> 1 lst[-4] IndexError: list index out of range In [10]: lst[3] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-10-298ffefac8cf> in <module>() ----> 1 lst[3] IndexError: list index out of range
修改元素的時候,若是超出索引範圍,也會引起IndexError。spa
3. 列表的操做.net
append:3d
insert:指針
extend:對象
(2)刪除元素
pop:
remove:
clear:
len函數
sort:
copy:
4. 切片
seq[start, end] [start, end)
5. 解包/封包