Hacker News new | past | comments | ask | show | jobs | submit login

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).




Unfortunately, Counter is not available before 2.7, so for many people it's a bit too early to require it.


There is a Python2.5 backport of collections.Counter() at http://code.activestate.com/recipes/576611/


This. 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.


Two years old is a long enough.


OSX 10.6.8 still has Python 2.6 as the system Python.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: