Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is actually quite elegant and useful. I've done similar things time and time again with code like so:

    defaultdict(lambda: defaultdict(int))
That allows me to organically build dictionaries (mainly for stat building) using operands like +=.


Also check out collections.Counter.

  x = collections.defaultdict(collections.Counter)
  x['foo']['bar'] += 1
  X['foo'].most_common(10)
 Etc.


Excellent! I didn't know about that one!




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

Search: