1、迭代器
(1)可迭代对象
1 s1 = ‘123‘ 2 for i in s1: 3 print(i)
示例结果:
D:\Python36\python.exe "E:/Python/课堂视频/day13视频与课堂笔记/day13课堂笔记/day13/02 迭代器.py" 1 2 3 True False Process finished with exit code 0
int object is not iterable
1 for i in 123: 2 print(i)
示例结果:
D:\Python36\python.exe "E:/Python/课堂视频/day13视频与课堂笔记/day13课堂笔记/day13/02 迭代器.py" Traceback (most recent call last): File "E:/Python/课堂视频/day13视频与课堂笔记/day13课堂笔记/day13/02 迭代器.py", line 8, in <module> for i in 123: TypeError: ‘int‘ object is not iterable Process finished with exit code 1
内部含有__iter__方法的就是可迭代对象,遵循可迭代协议。
1 dir 2 print(dir(‘123‘)) # ‘__iter__‘
结果:
1 D:\Python36\python.exe "E:/Python/课堂视频/day13视频与课堂笔记/day13课堂笔记/day13/02 迭代器.py" 2 [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘] 3 4 Process finished with exit code 0
D:\Python36\python.exe "E:/Python/课堂视频/day13视频与课堂笔记/day13课堂笔记/day13/02 迭代器.py" [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘] Process finished with exit code 0
2、生成器
3、知识点补充
4、列表推导式,生成器表达式
5、如何系统科学的学习Python
Python全栈__迭代器、生成器、知识点补充、列表推导式,生成器表达式、如何系统科学的学习Python
原文:https://www.cnblogs.com/ZN-225/p/9125049.html