[David Eppstein] > I'd probably use count_elements, but as Bob I. already said, I'd prefer > the dict to the transposed pairs of its items. An alternate dictionary constructor would provide that flexibility: def countkeys(cls, iterable): b = cls() for elem in iterable: [elem] = b.get(elem, 0) + 1 return b However, the added flexibility makes the use case more complicated: >>> from heapq import nlargest >>> words = (word for line in myfile for word in line.split()) >>> nlargest(3, pairwise(dict.countkeys(words).iteritems())) [(200, 'super'), (129, 'hero'), (103, 'villain')] That last line is much more involved than writing: >>> nlargest(3, count_elements(words)) I find the latter to be more readable and to be a more obvious tool for working with nlargest(), nsmallest(), sorted(), min(), and max(). Also, it does less work under the hood because it doesn't have to build two different tuples for each entry. OTOH, if you actually want to end-up with a dictionary, it is somewhat wasteful to build one, throw it away, and then rebuild it from the itemlist: >>> dict(count_elements(words)) Raymond
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