30/09/2018, 22:23
Nhờ giải thích về python generator
Mình mới học python thôi đến phần generator có chút không hiểu mong mọi người chỉ giúp
def integers():
"""Infinite sequence of integers."""
i = 1
while True:
yield i
i = i + 1
def squares():
for i in integers():
yield i * i
def take(n, seq):
"""Returns first n values from the given sequence."""
seq = iter(seq)
result = []
try:
for i in range(n):
result.append(seq.__next__())
except StopIteration:
pass
return result
print(take(5, squares()))
# prints [1, 4, 9, 16, 25]
Mình không hiểu từ phần def take…
Thanks for reading
Bài liên quan
Bỏ code vô cặp ``` để code được format nha bạn!
http://daynhauhoc.com/t/markdown-trong-daynhauhoc-com/
thanks bạn.lần sau mình sẽ chú ý hơn