Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Exercise 10.1 Revise a previous program as follows: Read and parse the “From” lines and
- pull out the addresses from the line. Count the number of messages from each person using a
- dictionary.
- After all the data has been read print the person with the most commits by creating a list of
- (count, email) tuples from the dictionary and then sorting the list in reverse order and print out the person who has the most commits.
- '''
- f1=open('mail.txt')
- d1=dict()
- for line in f1:
- words=line.split()
- if words[0]=='From':
- d1[words[1]]=d1.get(words[1],0)+1
- else:
- continue
- print d1
- list=[]
- for key,val in d1.items():
- list.append( (val, key) )
- list.sort(reverse=True)
- print list
- '''
- Exercise 10.2 This program counts the distribution of the hour of the day for each of the mes-
- sages. You can pull the hour from the “From” line by finding the time string and then splitting
- that string into parts using the colon character. Once you have accumulated the counts for each
- hour, print out the counts, one per line, sorted by hour as shown below.
- '''
- 1=open('mail.txt')
- d1=dict()
- for line in f1:
- words=line.split()
- if words[0]=='From':
- a=words[5].split(':')
- d1[a[0]]=d1.get(a[0],0)+1
- else:
- continue
- print d1
- list=[]
- for key,val in d1.items():
- list.append( (key,val) )
- list.sort(cmp=None, key=None, reverse=False);
- for key,val in list:
- print key,val
- '''
- Exercise 10.3 Write a function called most_frequent that takes a string and prints the let-
- ters in decreasing order of frequency. Find text samples from several different languages and
- see how letter frequency varies between languages. Compare your results with the tables at
- wikipedia.org/wiki/Letter_frequencies.
- '''
- def most_frequent(s):
- wordlist=list(s)
- d1=dict()
- for c in wordlist:
- d1[c]=d1.get(c,0)+1
- list1=[]
- for key,val in d1.items():
- list1.append( (val,key) )
- list1.sort(reverse=True);
- for key,val in list1:
- print key,val
- most_frequent('piramida')
Advertisement
Add Comment
Please, Sign In to add comment