Python Forum
Using dictionary to find the most sent emails from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using dictionary to find the most sent emails from a file
#7
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.





(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]
Reply


Messages In This Thread
RE: Using dictionary to find the most sent emails from a file - by siliusu - Apr-22-2021, 06:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  to find in dictionary given parameter 'name' and to output position Liki 10 1,416 Oct-08-2023, 06:38 AM
Last Post: Pedroski55
  dictionary output to text file (beginner) Delg_Dankil 2 1,221 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Updating dictionary in another py file tommy_voet 1 4,946 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,526 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,286 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,466 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  Sending Emails in Portuguese RenanPereira10 1 3,000 Jul-24-2020, 12:42 AM
Last Post: nilamo
  how to find 'cycle' for key-value pairs in a dictionary? junnyfromthehood 1 3,618 Sep-29-2019, 01:07 AM
Last Post: ichabod801
  how to put text file in a dictionary infected400 2 3,019 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 4,734 Aug-08-2018, 03:37 PM
Last Post: Vysero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020