Itertools wishlists
Raymond Hettinger
vze4rx4y at verizon.net
Mon Mar 14 13:52:21 EST 2005
> Steven> complex atomicity test). I also have the feeling that any
> Steven> complicated atomictiy test is more than a simple and-ing
> Steven> of several tests...
"Ville Vainio"
> I also have the feeling that if the atomicity criterion was any more
> complex in the API, the proposal would be shot down immediately on the
> grounds of not being fundamental enough as concept.
Would this meet your needs?
def flatten(iterable, atomic_iterable_types=(basestring,)):
iterstack = [iter(iterable)]
while iterstack:
for elem in iterstack[-1]:
if not isinstance(elem, atomic_iterable_types):
try:
it = iter(elem)
except TypeError:
pass
else:
iterstack.append(it)
break
yield elem
else:
iterstack.pop() # only remove iterator when it is exhausted
Raymond Hettinger
More information about the Python-list
mailing list