Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group/Count list of dictionaries based on value

I've got a list of Tokens which looks something like:

[{
    Value: "Blah",
    StartOffset: 0,
    EndOffset: 4
}, ... ]

What I want to do is get a count of how many times each value occurs in the list of tokens.

In VB.Net I'd do something like...

Tokens = Tokens.
GroupBy(Function(x) x.Value).
Select(Function(g) New With {
           .Value = g.Key,
           .Count = g.Count})

What's the equivalent in Python?

like image 947
Basic Avatar asked Apr 04 '13 15:04

Basic


People also ask

How do I sort a list of dictionaries by a value of the dictionary?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function. See the following article for more information.

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

The combination of above functionalities can be used to solve this problem. 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 get a list of all the values in a dictionary?

Get a list of values from a dictionary using List comprehension. Using List comprehension we can get the list of dictionary values. Here we are using a list comprehension to iterate in a dictionary by using an iterator. This will return each value from the key: value pair.

How do you count the number of items in a dictionary?

If you want to find the count number of items stored in a dictionary then you have to use the len() function where items are to be represented as key-value pairs.


1 Answers

IIUC, you can use collections.Counter:

>>> from collections import Counter
>>> tokens = [{"Value": "Blah", "SO": 0}, {"Value": "zoom", "SO": 5}, {"Value": "Blah", "SO": 2}, {"Value": "Blah", "SO": 3}]
>>> Counter(tok['Value'] for tok in tokens)
Counter({'Blah': 3, 'zoom': 1})

if you only need a count. If you want them grouped by the value, you could use itertools.groupby and something like:

>>> from itertools import groupby
>>> def keyfn(x):
        return x['Value']
... 
>>> [(k, list(g)) for k,g in groupby(sorted(tokens, key=keyfn), keyfn)]
[('Blah', [{'SO': 0, 'Value': 'Blah'}, {'SO': 2, 'Value': 'Blah'}, {'SO': 3, 'Value': 'Blah'}]), ('zoom', [{'SO': 5, 'Value': 'zoom'}])]

although it's a little trickier because groupby requires the grouped terms to be contiguous, and so you have to sort by the key first.

like image 98
DSM Avatar answered Sep 20 '22 07:09

DSM