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
#3
The From lines looks like this:
Output:
From alice@edu Thu Jun 16 16:12:12 2005 From bob@gov Thu Jun 16 18:13:12 2005 From ted@com Thu Jul 28 09:53:31 2005 From bob@gov Thu Jul 28 09:59:31 2005 From ted@com Thu Jul 28 15:53:31 2005
I got it from an example: doc/networkx-2.1/examples/drawing/unix_email.mbox
Output:
From alice@edu Thu Jun 16 16:12:12 2005 From: Alice <alice@edu> Subject: NetworkX Date: Thu, 16 Jun 2005 16:12:13 -0700 To: Bob <bob@gov> Status: RO Content-Length: 86 Lines: 5 Bob, check out the new networkx release - you and Carol might really like it. Alice From bob@gov Thu Jun 16 18:13:12 2005 Return-Path: <bob@gov> Subject: Re: NetworkX From: Bob <bob@gov> To: Alice <alice@edu> Content-Type: text/plain Date: Thu, 16 Jun 2005 18:13:12 -0700 Status: RO Content-Length: 26 Lines: 4 Thanks for the tip. Bob From ted@com Thu Jul 28 09:53:31 2005 Return-Path: <ted@com> Subject: Graph package in Python? From: Ted <ted@com> To: Bob <bob@gov> Content-Type: text/plain Date: Thu, 28 Jul 2005 09:47:03 -0700 Status: RO Content-Length: 90 Lines: 3 Hey Ted - I'm looking for a Python package for graphs and networks. Do you know of any? From bob@gov Thu Jul 28 09:59:31 2005 Return-Path: <bob@gov> Subject: Re: Graph package in Python? From: Bob <bob@gov> To: Ted <ted@com> Content-Type: text/plain Date: Thu, 28 Jul 2005 09:59:03 -0700 Status: RO Content-Length: 180 Lines: 9 Check out the NetworkX package - Alice sent me the tip! Bob >> bob@gov scrawled: >> Hey Ted - I'm looking for a Python package for >> graphs and networks. Do you know of any? From ted@com Thu Jul 28 15:53:31 2005 Return-Path: <ted@com> Subject: get together for lunch to discuss Networks? From: Ted <ted@com> To: Bob <bob@gov>, Carol <carol@gov>, Alice <alice@edu> Content-Type: text/plain Date: Thu, 28 Jul 2005 15:47:03 -0700 Status: RO Content-Length: 139 Lines: 5 Hey everyrone! Want to meet at that restaurant on the island in Konigsburg tonight? Bring your laptops and we can install NetworkX. Ted



from collections import defaultdict
from collections import Counter

# test = defaultdict(int)
# test["Not Existing Key"] -> 0
# text["Not Existing Key"] += 1
# then "Not Existing Key" -> 1


# The counter counts unique objects in a list or
# from other iterables. If it's a collection like a dict or defaultdict,
# the results are also copied


name = input("Enter file: ")
fh = open(name)
# a context manager is better

counts = defaultdict(int)
for line in fh:
    if line.startswith("From "):
        email = line.split(maxsplit=3)[1]
        # maxsplit limits the split of 3 elements.
        # from, email, rest ....
        # we need only the email, which is the second element
        # the third element is the rest of the line
        counts[email] += 1
        # defaultdict and counter supports this
        # if you use a defaultdict, then the initial datatype
        # must be int


# you've forgotten to close the file
# this could not happen with a context manager
fh.close()


print("Results:")
for email, count in counts.items():
    print(email, "->", count)


# we have already the Results in `counts`
# using Counter to reuse the data

counts2 = Counter(counts)
# Counter has the method most_common
print()
print("Top 5:")
for email, count in counts2.most_common(5):
    print(email, "->", count)
Counter could be use in the first place instead of defaultdict.
You can do this also manually, which is good to learn how to memorize elements.

If you make your own Counter, then use a set() as place to store seen E-Mails.

emails = ["a", "a", "c", "b"]
seen = set()
result = {}
for email in emails:
    if email in seen:
        result[email] += 1
    else:
        result[email] = 1
        seen.add(email)
        # a set uses add to add elements
        # a list uses append
        # a set has only unique elements
        # and is very fast in checking containment of an element in the set
I hope this helps a little to understand.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  to find in dictionary given parameter 'name' and to output position Liki 10 3,514 Oct-08-2023, 06:38 AM
Last Post: Pedroski55
  dictionary output to text file (beginner) Delg_Dankil 2 3,408 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Updating dictionary in another py file tommy_voet 1 6,381 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,934 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 3,039 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 3,327 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  Sending Emails in Portuguese RenanPereira10 1 3,644 Jul-24-2020, 12:42 AM
Last Post: nilamo
  how to find 'cycle' for key-value pairs in a dictionary? junnyfromthehood 1 4,422 Sep-29-2019, 01:07 AM
Last Post: ichabod801
  how to put text file in a dictionary infected400 2 3,778 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 6,284 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