Created
January 29, 2013 22:10
-
-
Save tombrad/4668419 to your computer and use it in GitHub Desktop.
aprenda python en 3 dias
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" | |
handle = open(name) | |
dic=dict() # defines an empty dictionary | |
for line in handle: | |
if not line.startswith("From ") : continue #select only lines with "From " | |
words=line.split() #turns string into list of words | |
word2=words[5] #select hour | |
word2=word2[:2] #slice 2 first characters | |
dic[word2]=dic.get(word2,0)+1 #accumulate in dictionary | |
flipped = list() | |
for clave, valor in dic.items(): | |
nvatupla = clave, valor #changes the order of key-value pair | |
flipped.append(nvatupla) #add to list flipped | |
flipped.sort() #sort list flipped | |
for clave,valor in flipped: | |
print clave,valor #print ordered list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment