Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have a list where some items don'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 an 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 Answer

0 votes
by (25.1k points)

You can do it 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}

Related questions

Browse Categories

...