Guest User

Untitled

a guest
Jul 10th, 2012
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. '''
  2. Exercise 10.1 Revise a previous program as follows: Read and parse the “From” lines and
  3. pull out the addresses from the line. Count the number of messages from each person using a
  4. dictionary.
  5. After all the data has been read print the person with the most commits by creating a list of
  6. (count, email) tuples from the dictionary and then sorting the list in reverse order and print out the person who has the most commits.
  7. '''
  8. f1=open('mail.txt')
  9. d1=dict()
  10. for line in f1:
  11.     words=line.split()
  12.     if words[0]=='From':
  13.         d1[words[1]]=d1.get(words[1],0)+1
  14.     else:
  15.         continue
  16. print d1
  17. list=[]
  18. for key,val in d1.items():
  19.     list.append( (val, key) )
  20. list.sort(reverse=True)
  21. print list
  22.  
  23. '''
  24. Exercise 10.2 This program counts the distribution of the hour of the day for each of the mes-
  25. sages. You can pull the hour from the “From” line by finding the time string and then splitting
  26. that string into parts using the colon character. Once you have accumulated the counts for each
  27. hour, print out the counts, one per line, sorted by hour as shown below.
  28. '''
  29. 1=open('mail.txt')
  30. d1=dict()
  31. for line in f1:
  32.     words=line.split()
  33.     if words[0]=='From':
  34.         a=words[5].split(':')
  35.         d1[a[0]]=d1.get(a[0],0)+1
  36.     else:
  37.         continue
  38. print d1
  39. list=[]
  40. for key,val in d1.items():
  41.     list.append( (key,val) )
  42. list.sort(cmp=None, key=None, reverse=False);
  43. for key,val in list:
  44.     print key,val
  45. '''
  46. Exercise 10.3 Write a function called most_frequent that takes a string and prints the let-
  47. ters in decreasing order of frequency. Find text samples from several different languages and
  48. see how letter frequency varies between languages. Compare your results with the tables at
  49. wikipedia.org/wiki/Letter_frequencies.
  50. '''
  51. def most_frequent(s):
  52.     wordlist=list(s)
  53.     d1=dict()
  54.     for c in wordlist:
  55.         d1[c]=d1.get(c,0)+1
  56.     list1=[]
  57.     for key,val in d1.items():
  58.         list1.append( (val,key) )
  59.     list1.sort(reverse=True);
  60.     for key,val in list1:
  61.         print key,val
  62.        
  63. most_frequent('piramida')
Advertisement
Add Comment
Please, Sign In to add comment