4 ms·
Overall, this is a nice post. There are two quibbles though. 1). For the most part, "c = collections.Counter()" is almost always better than "c = defaultdict
by raymondh 14y ago
Overall, this is a nice post. There are two quibbles though.
1). For the most part, "c = collections.Counter()" is almost always better than "c = defaultdict(int)"
* Counter only supplies missing values rather than automatically inserting them upon lookup.
* The Counter version is much clearer about what it is trying to do. The defaultdict version is cryptic to the uninitiated (understanding it entails knowing that it has a __missing__ method to insert values computed by a factory function and that int() with no arguments returns zero).
* The Counter version provides helpful methods such as "most_common(n)".
2). An ellipsis in Python is normally used in a much different way than shown in the article (it's used for an extended slice notation in NumPy).
- Camillo 14y agoUnfortunately, Counter is not available before 2.7, so for many people it's a bit too early to require it.
- MBlume 14y agoThis. On reading this post, I ran to my computer to replace defaultdict(int) with Counter in some of my code, only to find that I couldn't use it yet.
- njharman 14y agoTwo years old is a long enough.
- sitkack 14y agoOSX 10.6.8 still has Python 2.6 as the system Python.
- raymondh 14y agoThere is a Python2.5 backport of collections.Counter() at http://code.activestate.com/recipes/576611/ http://code.activestate.com/recipes/576611/