from collections import Iterable
items = [1, [2, 3, [4], 5], 6]
def flatten(items):
for x in items:
if isinstance(x, Iterable):
yield from flatten(x)
else:
yield x
print(*flatten(items))
Задачи по питону и машинному обучению: алгоритмы, функции, классы, регулярные выражения, итераторы, генераторы, ООП, исключения, numpy, pandas, matplotlib, scikit-learn, TensorFlow и др. #Python #ml
from collections import Iterable
items = [1, [2, 3, [4], 5], 6]
def flatten(items):
for x in items:
if isinstance(x, Iterable):
yield from flatten(x)
else:
yield x
print(*flatten(items))