Last active
December 11, 2015 22:08
-
-
Save tombrad/4667193 to your computer and use it in GitHub Desktop.
aprenda python en 3 días
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = "mbox-short.txt" # the name of the file with the text source | |
handle = open(name) # "store" the file temporally into a handle variable | |
cou=dict() # defines a dictionary named cou | |
correos=list() # defines a list called correos | |
#correos create a list with every mail address | |
for line in handle: # check for every line of the file with text | |
if not line.startswith("From:") : continue # if the line don´t start with "From:" pass to the next line | |
words=line.split() # else store the line in the list named words with every component | |
# separated by a space splitted as a different ellement | |
correos.append(words[1]) # words(1) is the second element of the list with the email address | |
#with correos (list with every email address) will generate a dictionary named cou | |
for acum in correos: # acum counts how many elements will have the dictionary cou | |
# accumulate the unique elements | |
if acum not in cou: # if the email is repeated skip,, don´t increase acum | |
cou[acum]=1 # else increase acum in 1 unit | |
else: # otherwise | |
cou[acum]=cou[acum]+1 # adds 1 to the value of cou (how many times an email address is repeated) | |
#then select the most frequent mail and how many times appear | |
maxval=None # initializate maxval to acumulate the maximun value (times of repitence) | |
maxname=None # ditto for the mail address | |
for kee,val in cou.items(): # inspect every pair address-value in the dictionary | |
if maxval==None: maxval=val # in the first inspection maxval equals the first value inspected | |
if maxval < val: # if the value currently inspected is lower than the current value in cou | |
maxval=val # replace maxval for current value of cou | |
maxname=kee # replace name of address with current value in maxname | |
print maxname, maxval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment