Skip to content

Instantly share code, notes, and snippets.

@longfin
Created December 2, 2011 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longfin/1422196 to your computer and use it in GitHub Desktop.
Save longfin/1422196 to your computer and use it in GitHub Desktop.
딱히 의도한건 아니지만 메모삼아...
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