How to clone a Python generator object? -
consider scenario:
#!/usr/bin/env python # -*- coding: utf-8 -*- import os walk = os.walk('/home') root, dirs, files in walk: pathname in dirs+files: print os.path.join(root, pathname) root, dirs, files in walk: pathname in dirs+files: print os.path.join(root, pathname)
i know example kinda redundant, should consider need use same walk
data more once. i've benchmark scenario , use of same walk
data mandatory helpful results.
i've tried walk2 = walk
clone , use in second iteration, didn't worked. question is... how can copy it? ever possible?
thank in advance.
you can use itertools.tee()
:
walk, walk2 = itertools.tee(walk)
note might "need significant storage", documentation points out.
Comments
Post a Comment