개발/Python
py4e 9.4 who has sent the greatest number of mail messages?
AimB
2020. 11. 29. 22:03
9.4 Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
어제 네이버 edwith의 파이썬 강의 추천을 올렸는데, 단점을 찾았다.
코세라와 다르게 실습을 직접 할 수 없다.
아무래도 코세라 실습 페이지를 코세라에서 직접 만들어서 그런가보다.
챕터별로 퀴즈도 풀 수 없다.
흠.... 원래 그냥 영자막으로 듣다가 한글 자막 띄우니 이해가 잘 되서 좋긴 한데
영어랑 한국어가 중첩되니 좀 어지럽기도 하고 그냥 코세라에서 완강할까 고민이 된다.
name = input("Enter file:")
fhand = open('mbox-short.txt')
counts = dict()
for line in fhand:
line = line.rstrip()
if line.startswith("From") and line.endswith('2008'):
spl = line.split()
mail = str(spl[1])
counts[mail] = counts.get(mail, 0) + 1
else:
continue
bigcount = None
bigword = None
for sender, count in counts.items():
if bigcount is None or count > bigcount:
bigword = sender
bigcount = count
print(bigword, bigcount)
here's my working code for ex_9_4