[Tutor] A dictionary question
Mats Wichmann
mats at wichmann.us
Mon Nov 15 14:06:51 EST 2021
On 11/15/21 01:00, Phil wrote:
> I think I have to start from scratch.
>
> The same pair of numbers has to exist twice in the row set plus each
> number (not a pair set) cannot exist in any other row set. That being
> the case, I now have to rethink the idea of using a pair list lookup
> table which is cumbersome and tedious to debug.
>
Doesn't sound too bad.
Count how many times the pairs appear in the various elements of your
rowlist. I'd probably do something like this:
from collections import Counter
pairs = Counter()
for s in set_list:
for r in row:
if s.issubset(r):
pairs[tuple(sorted(s))] += 1
since sets are unordered I would sort to get a reproducible pair, then
have to convert to tuple so it's hashable and able to serve as a
dictionary key.
Now walk through the counts, finding candidates - namely pairs with
counts of 2 (or whatever number you pick), and do a quick pass counting
up how many times each item of the pair appears in the rowlist elements.
If those counts aren't also two, your candidate is eliminated.
Remembering that Counter is a dict, you can start this loop like:
for item, count in pairs.items():
does this help?
More information about the Tutor
mailing list