Filter out elements that occur less times than a minimum threshold Filter out elements that occur less times than a minimum threshold python python

Filter out elements that occur less times than a minimum threshold


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}


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:

>>> ACounter({'a': 4, 'b': 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)