Count words in file
This commit is contained in:
parent
489a39b639
commit
8a69d379b9
1 changed files with 24 additions and 1 deletions
|
|
@ -1 +1,24 @@
|
|||
open(alice_in_wonderland.txt, 'r')
|
||||
aliceBook = open("thinkcspy/Chapter-12-Exercises/alice_in_wonderland.txt", "r")
|
||||
aliceDict = {}
|
||||
|
||||
for line in aliceBook:
|
||||
words = line.split()
|
||||
|
||||
for word in words:
|
||||
word = word.lower()
|
||||
newWord = ""
|
||||
|
||||
for char in word:
|
||||
if char.isalpha():
|
||||
newWord += char
|
||||
|
||||
word = newWord
|
||||
|
||||
if word not in aliceDict:
|
||||
aliceDict[word] = 1
|
||||
else:
|
||||
aliceDict[word] += 1
|
||||
|
||||
print(aliceDict)
|
||||
|
||||
aliceBook.close()
|
||||
|
|
|
|||
Loading…
Reference in a new issue