In the following code, if I use:
for line in fin:
It only executes for 'a'
But if I use:
wordlist = fin.readlines()
for line in wordlist:
Then it executes for a thru z.
But readlines()
reads the whole file at once, which I don't want.
How to avoid this?
def avoids():
alphabet = 'abcdefghijklmnopqrstuvwxyz'
num_words = {}
fin = open('words.txt')
for char in alphabet:
num_words[char] = 0
for line in fin:
not_found = True
word = line.strip()
if word.lower().find(char.lower()) != -1:
num_words[char] += 1
fin.close()
return num_words
You have three options:
the syntax
for line in fin
can only be used once. After you do that, you've exhausted the file and you can't read it again unless you "reset the file pointer" byfin.seek(0)
. Conversely,fin.readlines()
will give you a list which you can iterate over and over again.I think a simple refactor with
Counter
(python2.7+) could save you this headache:which will count the number of words in your file (1 word per line) that contain a particular character (which is what your original code does I believe ... Please correct me if I'm wrong)
You could also do this easily with a
defaultdict
(python2.5+):And finally, kicking it old-school -- I don't even know when
setdefault
was introduced...:Try: