[Tutor] Reading file and storing keys
moheem ilyas
dmjohnsonn at gmail.com
Thu Jun 28 02:19:47 CEST 2012
I am working on a problem from a book, Think Python, which I thought would
be fairly easy. The problem is:
Exercise 11.1. Write a function that reads the words in words.txt and
stores them as keys in a
dictionary. It doesn’t matter what the values are. Then you can use the in
operator as a fast way to
check whether a string is in the dictionary.
Note: words.txt is just a huge word list file if anyone is confused about
that
Here is my failed solution:
def tester():
fin = open('/home/moheem/Documents/words.txt', 'r')
value = 0
wordDict = dict()
for word in fin:
wordDict[word] = value
value = value + 1
fin.close()
There seems to be a logical error. That is, when I check a key, i.e. one of
the words from the file, is in the dictionary, I get false. (To check, I
use: 'aa' in wordDict). I think the problem is that the key does not
actually get placed in the dictionary, but the question is why?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120627/fcd3bfd4/attachment.html>
More information about the Tutor
mailing list