一、线性结构
list、tuple、str、bytes、bytearray,range()
都是顺序存储、顺序访问的、都是可迭代对象、都可以通过索引访问;被称为线性结构
可使用len()获取长度、可以切片,可以使用iter()将可迭代对象转换为迭代器
In [16]: r = range(8) In [17]: r Out[17]: range(0, 8) In [18]: i = iter(range(8)) In [19]: i Out[19]:In [20]: next(i) Out[20]: 0 In [21]: next(i) Out[21]: 1 In [23]: len(r) Out[23]: 8 In [24]: l = ["a", 0, 1] In [25]: i = iter(l) In [26]: type(i) Out[26]: list_iterator In [27]: next(i) Out[27]: 'a' In [28]: next(i) Out[28]: 0 In [29]: next(i) Out[29]: 1 In [30]: next(i) --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) in () ----> 1 next(i) StopIteration:
可迭代对象都可以用len获取长度,使用iter()转化成迭代器(iterator)
二、切片操作
通过索引获取获取某个片段的值
格式:
list[start:stop:step] # 包含start不包含stop;返回一个新的list,不会对原list修改
In [1]: lst = list(range(5)) In [2]: lst Out[2]: [0, 1, 2, 3, 4] In [3]: lst[3] Out[3]: 3 In [4]: lst[3:4] Out[4]: [3] In [6]: lst[1:5] Out[6]: [1, 2, 3, 4] In [7]: lst[1:5:2] Out[7]: [1, 3] In [8]: lst[1:5:6] Out[8]: [1] In [9]: lst[1:5:4] Out[9]: [1] In [10]: lst[1:5:3] Out[10]: [1, 4] In [11]: lst[1:5:-2] Out[11]: [] In [14]: lst[:] Out[14]: [0, 1, 2, 3, 4] In [15]: lst[:3] Out[15]: [0, 1, 2] In [16]: lst[3:] Out[16]: [3, 4] In [17]: lst[::] # star,stop,step都可以省略;默认为0,-0,1 Out[17]: [0, 1, 2, 3, 4] In [18]: lst[::2] Out[18]: [0, 2, 4] In [19]: lst[::-2] # 这时step可以使用负数 Out[19]: [4, 2, 0] In [20]: lst[::-1] Out[20]: [4, 3, 2, 1, 0] In [21]: lst[1:5:-1] Out[21]: [] In [22]: lst[:5:-1] Out[22]: [] In [23]: lst[0::-1] Out[23]: [0]
当start >= stop时,step为正时,返回空List;当step为负数时,反向
In [34]: lst[2:-1] # -1索引实际上等于len(list) + index,还是大于stop Out[34]: [2, 3] In [38]: lst[1:5] Out[38]: [1, 2, 3, 4] In [39]: lst[1:5:-2] Out[39]: [] In [40]: lst[5:1:-2] Out[40]: [4, 2]
三、解构与封装
1、解构
解构:按照元素顺序,把线性结构的元素复制给变量
In [69]: x = 1 In [70]: y = 2 In [71]: x, y Out[71]: (1, 2) In [72]: tmp = x In [73]: x = y In [74]: y = tmp In [75]: x, y Out[75]: (2, 1) # 解构 In [76]: x = 1 In [77]: y = 2 In [78]: x, y = y, x In [79]: x, y Out[79]: (2, 1) In [90]: lst = [1, 2] In [91]: first, second = lst In [92]: first, second Out[92]: (1, 2)
2、封装
定义一个tuple,可以省略括号;封装出来的一定是tuple
In [95]: t = 1, 2 In [96]: type(t) Out[96]: tuple
3、结构的变化
python3新增的特性
In [97]: lst = list(range(5)) In [98]: lst Out[98]: [0, 1, 2, 3, 4] In [99]: head, *mid, tail = lst In [100]: head Out[100]: 0 In [101]: tail Out[101]: 4 In [102]: mid Out[102]: [1, 2, 3] ## 加*号表示剩下的所有元素 In [103]: head, mid, *tail = lst In [104]: head Out[104]: 0 In [105]: mid Out[105]: 1 In [106]: tail Out[106]: [2, 3, 4] In [108]: *head, mid, tail = lst In [109]: head Out[109]: [0, 1, 2] In [110]: mid Out[110]: 3 In [111]: tail Out[111]: 4 In [112]: *head = lst File "", line 1 *head = lst ^ SyntaxError: starred assignment target must be in a list or tuple In [113]: head, *m1, *m2, tail = lst File " ", line 1 head, *m1, *m2, tail = lst ^ SyntaxError: two starred expressions in assignment In [114]: *m1, *m2, tail = lst File " ", line 1 *m1, *m2, tail = lst ^ SyntaxError: two starred expressions in assignment
总结:
元素按照顺序赋值给变量
变量和元素必须匹配
加星号的变量,可以接受任意个数元素,不管右边是什么type返回的都是一个list,可以是[]
加型号的变量不能单独出现
In [115]: lst = [0, 1, 2, 3] In [116]: v1, v2, v3, v4, v5 = lst --------------------------------------------------------------------------- ValueError Traceback (most recent call last)in () ----> 1 v1, v2, v3, v4, v5 = lst ValueError: not enough values to unpack (expected 5, got 4) In [117]: v1, v2, v3, v4 = lst In [118]: v1,v2,v3,v4 Out[118]: (0, 1, 2, 3) In [119]: v1,v2,v3,*v4= lst In [120]: v1,v2,v3,v4 Out[120]: (0, 1, 2, [3]) In [121]: v1,v2,v3,v4,*v5 = lst In [122]: v1,v2,v3,v4,v5 Out[122]: (0, 1, 2, 3, [])
常见用法:
In [130]: head, *_, tail = lst ## python中惯常使用单个下划线_表示丢弃该变量, ## 单个下划线也是Python中合法的标识符,但是如果不是要丢弃一个变量,通常不要用单个下划线表示一个变量 In [131]: head Out[131]: 0 In [132]: tail Out[132]: 3 In [133]: _ Out[133]: [1, 2] ## 多层级解构 In [14]: lst = [1, (2, 3), 5] In [15]: _, (_, var), _ = lst In [16]: var Out[16]: 3 In [17]: _ Out[17]: 5
没有解构,也可以活,但有了解构可以活的很美好
解构这个特性,被很多语言借鉴,如(ES6)