[Tutor] exercise

Matthew Ngaha chigga101 at gmail.com
Sat Dec 15 01:46:31 CET 2012


i have an assignment. the example given showed the opposite way to
achieve it but im a bit stuck, here's an explanation of the example.

it's a mailing list that will be kept in a default dict with sets as
value containers to ensure there are no duplicate email addresses. the
key will hold the email address and the value will hold the email
group in a set(). here is some code to better explain.


self.sort_email = defaultdict(set)

self.sort_email['mail at hotmail.com'].add('Friends')
self.sort_email['mail2 at gmail.com'].add('Friends')

self.sort_email['mail at hotmail.com'].add('Family') #duplicate in a
different group
self.sort_email['eddie at hotmail.com'].add('Family')

self.sort_email['ebay at ebay.com'].add('work')

so even if an email can be in different groups, using a set ensures
the same mail won't be sent twice to the same address


now sending the email, we have to make a list of the groups we want to
send mail to and make it a set()

group_list = ['Friends', 'Family']
group_list = set(group_list)

return {e for (e, g) in self.sort_email.items()
                if g & groups_list}

#this returns 'mail at hotmail.com', 'mail2 at hotmail.com' , 'eddie at hotmail.com'

e represents email addresses and g represents groups. the condition is
saying return only the email addresses that are in BOTH group_list and
the dictionary sets. after getting the emails from the return
statement, i send them off.


that was the example, now my assignment is the opposite. it wants me
to use groups as the keys in the dictionary and a list of email
addresses as the values.
it seems simple enough but i just cant get it. im on the verge of
giving up but i wondered if anyone could show or tell me how to
achieve this. the example said to do this just needs a small amount of
modifying the above.


More information about the Tutor mailing list