Created
December 2, 2011 07:29
-
-
Save longfin/1422196 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
from collections import deque, Iterable | |
def flatten(l): | |
""" | |
>>> [i for i in flatten([1, [2,3], [5, [6,7]]])] | |
[1, 2, 3, 5, 6, 7] | |
""" | |
queue = deque(l) | |
while len(queue) > 0: | |
p = queue.popleft() | |
if isinstance(p, Iterable): | |
for e in flatten(p): | |
yield e | |
else: | |
yield p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment