[Python-Dev] Re: Candidate Itertools
David Eppstein
eppstein at ics.uci.edu
Tue Jun 15 17:54:39 EDT 2004
In article <000a01c452b2$a6f13b20$bfaf2c81 at oemcomputer>,
"Raymond Hettinger" <raymond.hettinger at verizon.net> wrote:
> Feedback is requested for two prospective itertools:
>
> def count_elements(iterable):
> b = {}
> for elem in iterable:
> b[elem] = b.get(elem, 0) + 1
> return ((cnt, elem) for elem, cnt in b.iteritems())
>
>
> def pairswap(iterable):
> return ((b,a) for a,b in iterable)
I'd probably use count_elements, but as Bob I. already said, I'd prefer
the dict to the transposed pairs of its items. I'm not so sure why
pairswap would be better than the generator expression it replaces,
though.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-Dev
mailing list