Raymond Hettinger <rhettin...@users.sourceforge.net> added the comment:
Attaching an updated patch for Py2.7. * Kept OP's simple constructor call but renamed it from counts() to Counter(): item_counts = Counter('acabbacba') * Followed Guido's advice and avoided subclassing from defaultdict(). Instead, subclassed from dict, keeping its familiar API. * Avoided KeyError issue by defining __missing__(). * Added most_common() method to address a principal use case -- patterned after Smalltalk's sortedByCount() method. * Added elements() method requested by Alex Martelli -- patterned after C++, Smalltalk, and Knuth's examples. * Made necessary subclass overrides to dict methods: __repr__, update, fromkeys, and copy. * Wrote docstrings, doctests, and motivating examples. * Verified that instances are copyable, deepcopyable, and picklable. Working on docs and unittests. Nice example (most common words in a text file): >>> import re >>> words = re.findall('\w+', open('hamlet.txt').read().lower()) >>> Counter(words).most_common(10) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)] ---------- keywords: +patch stage: -> test needed type: -> feature request versions: +Python 2.7, Python 3.1 -Python 2.6 Added file: http://bugs.python.org/file12662/counter.diff _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue1696199> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com