[Tutor] A dictionary question

Phil phillor9 at gmail.com
Sat Nov 13 00:04:43 EST 2021


Thank you for taking the time to look at this.

I'd like to find the number of pairs in the rows list of sets. The pair 
sets are {7, 3} and {4, 6} because they appear twice each. I'd also like 
to have count_dict dictionary show the two rows where the sets were 
found. What I have is this:

{'row': 5, 'count': 2, 'set': {4, 6}}

Which shows the last row, the count and the set found. I'd like the 
dictionary to also show where the other set {4, 6} was found which is row 1.

This is my best effort so far. I also tried counter from the collections 
module but I doesn't seem to work with sets, at least I couldn't get it 
to work with sets.

rows = [{7,3},{4,6,8},{7,8},{9,3},{7,3},{4,6,3}]

set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9},
             {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9},
             {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9},
             {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9},
             {5, 6}, {5, 7}, {5, 8}, {5, 9},
             {6, 7}, {6, 8}, {6, 9},
             {7, 8}, {7, 9},
             {8, 9}
             ]

count_dict = {}

for i in range(len(set_list)):
     count = 0
     for j in range(6):
         if set_list[i].issubset(rows[j]):
             found_set = set_list[i]
             count += 1
             count_dict['row'] = j
             count_dict['count'] = count
             count_dict['set'] = found_set

     if count == 2:
         print(set_list[i], ' is in row ',count, ' times')
         print(count_dict)

Thank you again.

-- 
Regards,
Phil



More information about the Tutor mailing list