Filter out elements that occur less times than a minimum threshold

14,469

Solution 1

Build your Counter, then use a dict comprehension as a second, filtering step.

{x: count for x, count in A.items() if count >= min_threshold}
# {'a': 4, 'b': 3}

Solution 2

You could remove the keys from the dictionary that are below 3:

for key, cnts in list(A.items()):   # list is important here
    if cnts < min_threshold:
        del A[key]

Which gives you:

>>> A
Counter({'a': 4, 'b': 3})

Solution 3

As covered by Satish BV, you can iterate over your Counter with a dictionary comprehension. You could use items (or iteritems for more efficiency and if you're on Python 2) to get a sequence of (key, value) tuple pairs. And then turn that into a Counter.

my_dict = {k: v for k, v in A.iteritems() if v >= min_threshold}
filteredA = Counter(my_dict)

Alternatively, you could iterate over the original Counter and remove the unnecessary values.

for k, v in A.items():
    if v < min_threshold:
        A.pop(k)

Solution 4

This looks nicer:

{ x: count for x, count in A.items() if count >= min_threshold }
Share:
14,469
Satish Bandaru
Author by

Satish Bandaru

iOS Developer @WalmartLabs India

Updated on June 03, 2022

Comments

  • Satish Bandaru
    Satish Bandaru almost 2 years

    After trying to count the occurrences of an element in a list using the below code

    from collections import Counter
    A = ['a','a','a','b','c','b','c','b','a']
    A = Counter(A)
    min_threshold = 3
    

    After calling Counter on A above, a counter object like this is formed:

    >>> A
    Counter({'a': 4, 'b': 3, 'c': 2})
    

    From here, how do I filter only 'a' and 'b' using minimum threshold value of 3?