24

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?

smci
  • 32,567
  • 20
  • 113
  • 146
Satish Bandaru
  • 266
  • 1
  • 2
  • 9

4 Answers4

28

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}
cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    This is suboptimal, there's no need to re-lookup every `A[x]` to get that item's `count`, just iterate directly over `for x, count in A.items()` – smci Jun 04 '20 at 23:19
4

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)
Michael Currin
  • 615
  • 5
  • 14
4

This looks nicer:

{ x: count for x, count in A.items() if count >= min_threshold }
smci
  • 32,567
  • 20
  • 113
  • 146
Ido Zehori
  • 43
  • 2
  • 1
    In Python 3, you need to iterate over `for x, count in A.items()` to avoid `ValueError: too many values to unpack (expected 2)` – smci Jun 04 '20 at 23:18
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:

>>> A
Counter({'a': 4, 'b': 3})
MSeifert
  • 145,886
  • 38
  • 333
  • 352