列表、元组和字符串的共同点:
--都可以通过索引的到每一个元素
--默认索引值总是从0开始
--可以通过分片的方法得到一个范围内的元素的集合
--有很多共同的操作符(重复操作符、拼接操作符、成员关系操作符)故这仨统称为序列
一、list() __把一个可迭代对象转换为列表
>>> 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 | | ...
【解释】list() -> new empty list __创建空列表
list(iterable) -> new list initialized from iterable's items __把一个可迭代对象转
换为列表'''创建空列表''' >>> a = list() >>> a [] '''将字符串转换为列表''' >>> b = 'I love chuan.521' >>> b 'I love chuan.521' >>> b = list(b) >>> b ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'c', 'h', 'u', 'a', 'n', '.', '5', '2', '1'] '''将元组转换为列表''' >>> c = (1, 1, 2, 3, 5, 8, 13, 21, 34) >>> c = list(c) >>> c [1, 1, 2, 3, 5, 8, 13, 21, 34]
二、tuple() __把一个可迭代对象转换为元组
>>> help(tuple) Help on class tuple in module builtins: class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple initialized from iterable's items
【解释】同list。
三、str(obj) __把obj对象转换为字符串
>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str
【解释】同list。
四、len(sub)返回sub的长度
五、max() __返回序列或者参数中的最大值
六、min() __返回序列或者参数中的最小值>>> c = (1, 1, 2, 3, 5, 8, 13, 21, 34) >>> max(c) 34 >>> number = [1, 18, 13, 0, -98, 34, 54, 76, 32] >>> max(number) 76 >>> b = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'c', 'h', 'u', 'a', 'n', '.', '5', '2', '1'] >>> max(b) 'v' >>> chars = '1234567890' >>> min(chars) '0'
【问题】why ,max(b) = 'v' ?
【解释】ASCII码的顺序
【注意】:不同类型不可比较!
>>> number = [1, 18, 13, 0, -98, 34, 54, 76, 32] >>> number.append('a') >>> number [1, 18, 13, 0, -98, 34, 54, 76, 32, 'a'] >>> max(number) Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> max(number) TypeError: '>' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
max(number)
TypeError: '>' not supported between instances of 'str' and 'int'
【拓展】max实现原理:
max = tuple1[0] for each in tuple1: if each > max: max = each return max
七、sum(iterable[, start=0]) __返回序列iterable和可选参数start的总和
>>> tuple2 = (3.1, 4.5, 6.1) >>> sum(tuple2) 13.7 >>> number = [1, 18, 13, 0, -98, 34, 54, 76, 32, 'a'] >>> sum(number) Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> sum(number) TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> number.pop() 'a' >>> sum(number) 130 >>> sum(number, 8) 138
八、sorted() __排序【比内置函数多了ed】
>>> number = [1, 18, 13, 0, -98, 34, 54, 76, 32] >>> sorted(number) [-98, 0, 1, 13, 18, 32, 34, 54, 76] >>> tuple2 = (3.1, 4.5, 6.1) >>> sorted(tuple2) [3.1, 4.5, 6.1]
九、reversed() __翻转位置【比内置函数多了ed】
>>> number = [1, 18, 13, 0, -98, 34, 54, 76, 32] >>> reversed(number) <list_reverseiterator object at 0x000001C6775E5438> '''一会讲,阿拉咕七''' '''可以用list方法转换''' >>> list(reversed(number)) [32, 76, 54, 34, -98, 0, 13, 18, 1]
十、enumerate() __返回index值(索引值)和item值
>>> enumerate(number) <enumerate object at 0x000001C677717288> '''出现了和【reversed】相同的情况。那就再用同样的方法''' >>> list(enumerate(number)) [(0, 1), (1, 18), (2, 13), (3, 0), (4, -98), (5, 34), (6, 54), (7, 76), (8, 32)]
十一、zip() __返回由各个参数的序列组成的元组
>>> a = [1, 2, 3, 4, 5, 6, 7, 8] >>> b = [4, 5, 6, 7, 8] >>> zip(a, b) <zip object at 0x000001C677738148> >>> list(zip(a, b)) [(1, 4), (2, 5), (3, 6), (4, 7), (5, 8)] '''a和b在一起了!!!''' '''但是多出来的就没有配对了'''