Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

item frequency in a python list of dictionaries

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?

like image 955
dochead Avatar asked Jun 28 '09 20:06

dochead


People also ask

How do you count the frequency of an element in a list Python dictionary?

A simple approach would be to iterate over the list and use each distinct element of the list as a key of the dictionary and store the corresponding count of that key as values.

How do I count the number of dictionaries in a list Python?

In this, we perform iteration using list comprehension and test for dictionary using isinstance(). The combination of above functionalities can be used to solve this problem. In this, we also solve the problem of inner nesting using recursion.

How do you count occurrences of all elements in a list Python?

Method 4: Count occurrences of an element in a list Using countof() Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value.

How do you find the frequency of unique values in a list?

You can use the combination of the SUM and COUNTIF functions to count unique values in Excel. The syntax for this combined formula is = SUM(IF(1/COUNTIF(data, data)=1,1,0)). Here the COUNTIF formula counts the number of times each value in the range appears.


2 Answers

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

like image 112
Alex Martelli Avatar answered Sep 17 '22 16:09

Alex Martelli


items = [{'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}]

global_dict = {}

for item in items:
    for key, value in item.items():
        if not global_dict.has_key(key):
            global_dict[key] = {}

        if not global_dict[key].has_key(value):
            global_dict[key][value] = 0

        global_dict[key][value] += 1

print global_dict

Simplest solution and actually tested.

like image 40
tefozi Avatar answered Sep 17 '22 16:09

tefozi