Created
May 25, 2020 14:31
-
-
Save jigi-33/fa76a2fc3149a2c29bc214a895f38bf8 to your computer and use it in GitHub Desktop.
Итераторы-генераторы
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ИТЕРАТОРЫ-ГЕНЕРАТОРЫ | |
""" | |
# Написать циключескую функцию-генератор | |
def cycle(iters): | |
while True: | |
for it in iters: | |
yield it | |
q = cycle([1,3,5]) | |
for i in range(10): | |
print(next(q)) | |
# Написать chain, которая последовательно итерирует переданные ему объекты (генератор по всему, что подсунут): | |
def chain(*args): | |
for arg in args: | |
yield from arg | |
q = chain([1,2,3],(5,6,7)) | |
print('\n#2\nq = chain([1,2,3],(5,6,7))') | |
for i in range(9): | |
print(next(q)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment