Created
January 18, 2017 03:35
-
-
Save wqlin/250d9dc82b4e35ccf6f1b3bee6edfdf7 to your computer and use it in GitHub Desktop.
flatten python's nested sequences
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
from collections import Iterable | |
def flatten(items, ignore_types=(str, bytes)): | |
"""flatten a nested sequence | |
implement it based on Python Cookbook(version 3) 4.14 Flattening a Nested Sequence""" | |
for x in items: | |
if isinstance(x, Iterable) and not isinstance(x, ignore_types): | |
yield from flatten(x, ignore_types) | |
else: | |
yield x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment