Apr-22-2021, 07:39 AM
The From lines looks like this:
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.
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.mboxOutput: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 setI 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!
All humans together. We don't need politicians!