A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2004-June/045432.html below:

[Python-Dev] Candidate Itertools

[Python-Dev] Candidate Itertools [Python-Dev] Candidate ItertoolsRaymond Hettinger python at rcn.com
Wed Jun 16 06:49:24 EDT 2004
> > 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())

[Michael Chermside]
> Is the behavior of count_elements() exactly the same as
> the given python equivalent when one (or more) of the
> elements in the iterable is not hashable?

That was the original idea; however, we could easily keep a separate
list of identity comparisons for unhashables:

b = {}
keys = []
counts = []
for elem in iterable:
    try:
        b[elem] = b.get(elem, 0) + 1
    except TypeError:
        i = keys.find(elem)
        if i == -1:
            keys.append(elem)
            counts.append(1)
        else:
            counts[i] += 1
return zip(counts, keys) + tuple(pairwise(b.iteritems()))



Raymond Hettinger


More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4