Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Group By and filter for attribute value missing

Tags:

python

I have a list where some items doesn't have an attribute like item["transitioned_to"] = 'Ended' and I would like to count how many items are missing this value.

I'm able only to count how many items HAS "Ended" attribute: data_filtered = list(filter(lambda x: x['transitioned_to'] == "Ended", steps)) - each Ended is relative to single execution_id.

How is possible to aggregate this list by execution_sid and count how many items are missing item["transitioned_to"] = 'Ended'?

As input example:

[{
    'execution_sid': 'sid1',
    'transitioned_from': 'step_a',
    'transitioned_to': 'step_b',
}, {
    'execution_sid': 'sid1',
    'transitioned_from': 'step_b',
    'transitioned_to': 'Ended',
}, {
    'execution_sid': 'sid2',
    'transitioned_from': 'step_a',
    'transitioned_to': 'step_b',
}]

In this example, should return 1 for each case: 1 HAS ended and 1 HASN'T ended. Is possible to perform this count using python?


1 Answers

With collections.defaultdict object:

from collections import defaultdict

lst = [{'execution_sid': 'sid1', 'transitioned_from': 'step_a', 'transitioned_to': 'step_b', },
       {'execution_sid': 'sid1', 'transitioned_from': 'step_b', 'transitioned_to': 'Ended', },
       {'execution_sid': 'sid2', 'transitioned_from': 'step_a', 'transitioned_to': 'step_b', }]

res = defaultdict(int)
for d in lst:
    res[d['execution_sid']] += d['transitioned_to'] != 'Ended'

print(dict(res))

The output (aggregated by execution_sid):

{'sid1': 1, 'sid2': 1}
like image 137
RomanPerekhrest Avatar answered Apr 02 '26 04:04

RomanPerekhrest