def flatmap(func, iterable):
"""
Map function to iterable and flatten.
>>> f = lambda n: str(n) * n
>>> list( flatmap(f, [1, 2, 3]) )
['1', '2', '2', '3', '3', '3']
>>> list( map(f, [1, 2, 3]) ) # map instead of flatmap
['1', '22', '333']
:param function func: Function to map on iterable.
:param iterable iterable: Any iterable, e.g. list, range, ...
:return: Iterator of iterable elements transformed via func and flattened.
:rtype: Iterator
"""
return itt.chain.from_iterable(map(func, iterable))
评论列表
文章目录