python开发基础迭代器和生成器

Posted Amfc-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python开发基础迭代器和生成器相关的知识,希望对你有一定的参考价值。

# 迭代器和生成器
from typing import List

l: List[int] = [1, 2, 3]
# for循环
"""
for i in l: # l.__iter__() /l.__iter__.__next__()
print(i)
"""

# while循环
"""
index = 0
while index < len(l):
print(l[index])
index += 1
"""

# 用__next__()
"""
iter_1 = l.__iter__() # 迭代器协议,生成可迭代对象
print(iter_1.__next__())
print(iter_1.__next__())
print(iter_1.__next__())

"""

# 解析__next__
"""
x = \'hello\'
iter_tools = x.__iter__()
print(iter_tools)
print(iter_tools.__next__())
print(iter_tools.__next__())
print(iter_tools.__next__())
print(iter_tools.__next__())
print(iter_tools.__next__())

"""

以上是关于python开发基础迭代器和生成器的主要内容,如果未能解决你的问题,请参考以下文章

Python基础 ( 六 ) —— 迭代器和生成器

python基础-迭代器和生成器

Python成长之路第五篇:Python基础之迭代器和生成器

python基础之三大器中迭代器和生成器

1.17 Python基础知识 - 迭代器和生成器初识

python---基础知识回顾迭代器和生成器