Message79461
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)] |
|
Date |
User |
Action |
Args |
2009-01-09 11:04:39 | rhettinger | set | recipients:
+ rhettinger, gvanrossum, bethard |
2009-01-09 11:04:39 | rhettinger | set | messageid: <1231499079.32.0.729203571142.issue1696199@psf.upfronthosting.co.za> |
2009-01-09 11:04:38 | rhettinger | link | issue1696199 messages |
2009-01-09 11:04:37 | rhettinger | create | |
|