item frequency in a python list of dictionaries

dictionary, python

Solution

`collections.defaultdict` from the standard library to the rescue:

from collections import defaultdict

LofD = [{'name': 'johnny', 'surname': 'smith', 'age': 53},
 {'name': 'johnny', 'surname': 'ryan', 'age': 13},
 {'name': 'jakob', 'surname': 'smith', 'age': 27},
 {'name': 'aaron', 'surname': 'specter', 'age': 22},
 {'name': 'max', 'surname': 'headroom', 'age': 108},
]

def counters():
  return defaultdict(int)

def freqs(LofD):
  r = defaultdict(counters)
  for d in LofD:
    for k, v in d.items():
      r[k][v] += 1
  return dict((k, dict(v)) for k, v in r.items())

print freqs(LofD)

emits

{'age': {27: 1, 108: 1, 53: 1, 22: 1, 13: 1}, 'surname': {'headroom': 1, 'smith': 2, 'specter': 1, 'ryan': 1}, 'name': {'jakob': 1, 'max': 1, 'aaron': 1, 'johnny': 2}}

as desired (order of keys apart, of course -- it's irrelevant in a dict).

Problem

Ok, so I have a list of dicts: ``` [{'name': 'johnny', 'surname': 'smith', 'age': 53}, {'name': 'johnny', 'surname': 'ryan', 'age': 13}, {'name': 'jakob', 'surname': 'smith', 'age': 27}, {'name': 'aaron', 'surname': 'specter', 'age': 22}, {'name': 'max', 'surname': 'headroom', 'age': 108}, ] ``` and I want the 'frequency' of the items within each column. So for this I'd get something like: ``` {'name': {'johnny': 2, 'jakob': 1, 'aaron': 1, 'max': 1}, 'surname': {'smith': 2, 'ryan': 1, 'specter': 1, 'headroom': 1}, 'age': {53:1, 13:1, 27: 1. 22:1, 108:1}} ``` Any modules out there that can do stuff like this?

Original source