/
ssstrekoza
/
Iterators_Generators_Yield
Обзор
Документация
Войти
/
ssstrekoza
/
Iterators_Generators_Yield
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
1.py
45 строк
1 KB
Elena Dekho
Initial commit
14 ноя 2025, 06:50
14 ноя 2025, 06:50
e0ae736
Код
Авторство
О чём код?
class FlatIterator: def __init__(self, list_of_list): self.list_of_list = list_of_list # Индекс подсписка self.outer_index = 0 # Индекс элемента внутри подписка self.inner_index = 0 def __iter__(self): return self def __next__(self): # Пропускаем пустые подсписки while self.outer_index < len(self.list_of_list): if self.inner_index < len(self.list_of_list[self.outer_index]): item = self.list_of_list[self.outer_index][self.inner_index] self.inner_index += 1 return item else: # Подписок закончился - переходим к следующему self.outer_index += 1 self.inner_index = 0 raise StopIteration def test_1(): list_of_lists_1 = [ ['a', 'b', 'c'], ['d', 'e', 'f', 'h', False], [1, 2, None] ] for flat_iterator_item, check_item in zip( FlatIterator(list_of_lists_1), ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None] ): assert flat_iterator_item == check_item assert list(FlatIterator(list_of_lists_1)) == ['a', 'b', 'c', 'd', 'e', 'f', 'h', False, 1, 2, None] if __name__ == '__main__': test_1()