| import urllib.request, urllib.parse, urllib.error
lines = []
with urllib.request.urlopen('http://data.pr4e.org/romeo.txt') as f:
for line in f:
lines.append(line.decode().strip())
lines
|
['But soft what light through yonder window breaks', 'It is the east and Juliet is the sun', 'Arise fair sun and kill the envious moon', 'Who is already sick and pale with grief'] |
import urllib.request, urllib.parse, urllib.error
word_counts = {}
with urllib.request.urlopen('http://data.pr4e.org/romeo.txt') as f:
for line in f:
words = line.decode().split()
for word in words:
word_counts[word] = word_counts.get(word, 0) + 1
sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
|
[('is', 3), ('the', 3), ('and', 3), ('sun', 2), ('But', 1), ('soft', 1), ('what', 1), ('light', 1), ('through', 1), ('yonder', 1), ('window', 1), ('breaks', 1), ('It', 1), ('east', 1), ('Juliet', 1), ('Arise', 1), ('fair', 1), ('kill', 1), ('envious', 1), ('moon', 1), ('Who', 1), ('already', 1), ('sick', 1), ('pale', 1), ('with', 1), ('grief', 1)] |
|