Compare Duplicate File Inventories with Python Counter
An asset export expects two occurrences of hero.png and one of button.png. The observed manifest contains only one hero entry. Comparing sets says the inventories match because a set discards repeated values. A count comparison can identify the missing occurrence.
These examples use Python 3.10 or later. Each input is a finite iterable of filename strings, with one string per manifest entry. The comparison concerns manifest occurrences; it does not inspect file bytes or establish whether two paths refer to the same physical file.
Reproduce the Lost Duplicate
Run this standalone example:
Counter stores each distinct filename as a key and its occurrence count as the value. Input order does not affect count equality. Missing keys read as zero, so a missing filename can be compared without manually initializing it. The official
Counter reference
describes the counting and arithmetic operations used here.
Report Missing and Extra Occurrences
Save this helper as inventory_diff.py:
Counter subtraction returns a new counter containing only positive differences. The first direction reports expected occurrences that were absent; the reverse direction reports unexpected occurrences. Neither operation modifies its operands.
The helper consumes each input once to build its counts. Pass separate iterators for independent inputs; passing the same one-shot iterator twice leaves the second pass exhausted. Pass a sequence of filenames rather than a single filename string, which would count individual characters. A mapping input initializes counts directly instead of counting keys as repeated records, so keep this helper’s input contract to filename iterables.
Run this beside the helper:
| |
Sorting the item pairs gives the report a predictable filename order. total() sums occurrence counts; len(extra) would count distinct unexpected filenames, giving one rather than two in this example. The counters retain counts without expanding them into potentially large lists of repeated filenames.
Preserve Signs Only When Needed
The mutating subtract() method has different behavior from the - operator. This standalone example keeps a signed balance where positive means missing and negative means extra:
subtract() changes the existing counter, returns None, and retains zero and negative counts. For the helper’s two positive-only reports, subtraction operators express the desired result directly. If you use a signed balance, inspect its values: a stored zero entry still contributes to its key count and can make the container truthy even when every balance is zero.
Test Counts and Input Boundaries
Save this as test_inventory_diff.py beside the helper and run python test_inventory_diff.py:
| |
The reorder case verifies that this is a count comparison rather than a sequence comparison. The duplicate cases catch a mistaken conversion to sets. Separate iterator inputs exercise single-pass consumption without depending on list indexing.
Define Filename Identity Before Counting
String equality determines which entries share a count. Hero.png, hero.png, and ./hero.png are different keys here. If the manifest format defines a canonical spelling, normalize both inputs according to that contract before counting. Blindly lowercasing paths can merge names that are distinct on a case-sensitive filesystem.
This report intentionally loses sequence order and entry locations. If the next step must identify the exact bad manifest lines, preserve line numbers separately. Memory grows with the number of distinct filenames, so using iterators avoids retaining the original input lists but does not make counting constant-space. Use this approach when duplicate quantities matter and the distinct-name inventory fits in memory.