本节内容: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. 解包/封包