KKAIROS
Practical guideIllustrative3 min read

Python Counter: count items

collections.Counter(iterable) counts how often each item appears. most_common(n) returns the n most frequent items as pairs.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-83-A: Counter counts items and most_common returns the top pair

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
from collections import Counter
c = Counter('banana')
print(c['a'], c['n'], c['b'], c['z'])
print(c.most_common(1))
EOS

Command:

python3 s.py

Recorded output:

3 2 1 0
[('a', 3)]

Exit status: 0. The output matches the expected text written before the run.

TE-83-B: update adds counts to a Counter, and the values add up to the number of items counted

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
from collections import Counter
c = Counter([1, 1, 2])
c.update([2, 2])
print(c)
print(sum(c.values()))
EOS

Command:

python3 s.py

Recorded output:

Counter({2: 3, 1: 2})
5

Exit status: 0. This status and the output match what was written before the run.

TE-83-C: expected non-zero exit: Counter of an integer raises TypeError because an integer is not iterable

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
from collections import Counter
Counter(5)
EOS

Command:

python3 s.py

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "/tmp/b22-jyf561ga/work/s.py", line 2, in <module>
    Counter(5)
  File "/usr/lib/python3.10/collections/__init__.py", line 577, in __init__
    self.update(iterable, **kwds)
  File "/usr/lib/python3.10/collections/__init__.py", line 670, in update
    _count_elements(self, iterable)
TypeError: 'int' object is not iterable

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-83-B: the counts after the update are 2 for the number 1 and 3 for the number 2, and the values add up to 5. TE-83-C: Counter(5) stopped with exit status 1. The last line of standard error is TypeError: 'int' object is not iterable.

Limits

TE-83-A counts a string and TE-83-B counts numbers. Order of equal counts was not tested.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-83-A | Counter counts items and most_common returns the top pair | 0 | PASS | 6af42e613529 | | TE-83-B | update adds counts to a Counter, and the values add up to the number of items counted | 0 | PASS | 39ec32454fdf | | TE-83-C | expected non-zero exit: Counter of an integer raises TypeError because an integer is not iterable | 1 | PASS | dc5e7aa65aa5 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides