KKAIROS
Practical guideIllustrative3 min read

Python: count with a dict or Counter, and meet sets and tuples

A dictionary or Counter counts occurrences; a set removes duplicates; a tuple is an immutable sequence. Set display order is not fixed.

Published:

Updated:

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-46-A: set removes duplicates but does not promise display order; Counter counts occurrences

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "from collections import Counter; print(sorted(set(['b','a','b']))); c=Counter('abracadabra'); print(c['a'], len(c)); print(sorted(c.items()))"

Recorded output:

['a', 'b']
5 5
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]

TE-46-B: a Counter returns 0 for a missing key and does not add that key

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

cat > s.py <<'EOS'
from collections import Counter
c = Counter('aab')
print(c['z'], 'z' in c, len(c))
EOS

Command:

python3 s.py

Recorded output:

0 False 2

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

TE-46-C: expected non-zero exit: a list cannot go into a set because it is not hashable

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

cat > s.py <<'EOS'
print(set([[1]]))
EOS

The file ../err.txt sits one level above the working folder, inside the same throwaway tree. The output block below shows only the last line of the error; the full traceback went to ../err.txt.

Command:

python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rc

Recorded output:

TypeError: unhashable type: 'list'

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

Reading the recorded output

The first line is sorted(set(['b','a','b'])), which gave ['a', 'b']: the set kept one b, and sorted put the items in order. The second line holds two numbers for Counter('abracadabra'). c['a'] is 5, the number of a letters in the text, and len(c) is 5, the number of different letters. The last line lists the pairs in sorted order: a 5, b 2, c 1, d 1, r 2. Those counts add up to 11, which is the length of the word abracadabra.

TE-46-B: for Counter('aab'), c['z'] gave 0, 'z' in c gave False, and len(c) stayed 2, so reading a missing key returned 0 and did not add it. TE-46-C: putting a list into a set stopped with TypeError: unhashable type: 'list', with exit status 1.

Ad

Limits

Set display order is not fixed. The counts come from the input shown.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-46-A | set removes duplicates but does not promise display order; Counter counts occurrences | 0 | PASS | b737414675b1 | | TE-46-B | a Counter returns 0 for a missing key and does not add that key | 0 | PASS | 9b40b550abff | | TE-46-C | expected non-zero exit: a list cannot go into a set because it is not hashable | 1 | PASS | cedb13bf6d8f |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides