itertools.flatten()? and copying generators/iterators.
Alex Martelli
aleax at aleax.it
Wed Oct 29 15:58:01 EST 2003
Peter Otten wrote:
...
> def flatten_dict(s, isScalar):
> try:
> scalar = isScalar[s.__class__]
> except KeyError:
> t = s.__class__
> try:
> s = iter(s)
> except TypeError:
> scalar = isScalar[t] = True
> else:
> scalar = isScalar[t] = False
>
> if scalar:
> yield s
> else:
> for elem in s:
> for subelem in flatten_dict(elem, isScalar):
> yield subelem
Neat: "caching" (aka memoizing) IS a good general technique
for optimization, and this is a cool and un-obvious application.
Alex
More information about the Python-list
mailing list