Apr-22-2021, 06:07 PM
Thank you for your reply so much!
I tried the code. It shows traceback on "counter[address] += 1",saying it must be integers or slices, not str.
I tried the code. It shows traceback on "counter[address] += 1",saying it must be integers or slices, not str.
(Apr-22-2021, 03:30 PM)perfringo Wrote: This is homework so special attention must be paid to terms and conditions.
- The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail.
- The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file.
- After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
I have trouble understanding what is 'maximum loop', therefore I will use built-in max.
counter = dict() with open('mbox-short.txt', 'r') as f: for line in f: if line.startswith('From '): address = line.split(maxsplit=2)[1] try: counter[address] += 1 except KeyError: counter[address] = 1 print(max(counter, key=lambda rec: rec[1])) # [email protected]