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.

Trying to figure this one out, but no luck so far. I am trying to get the function to open words.txt, count the length of the word, use word as key and the len(word) as the value for input into the dictionary. I'm pretty sure this is not the right way to add these items into the dictionary but I can't find it anywhere only how to manually add them.

If I type engdicdefine()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
engdicdefine()
File "C:\Users\Jeremy\Google Drive\Trident Module saves\Duncan - Module 3 Case\Module 3 Think Python Chap11.py", line 65, in engdicdefine
engwords=dict(wordcount)
ValueError: dictionary update sequence element #0 has length 4; 2 is required

If I type engwords, I get:
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
engwords
NameError: name 'engwords' is not defined

def engdicdefine():
        fin = open('words.txt')
        line = fin.readline()
        word = line.strip()
        count=0
        for word in fin:
            count=len(word)
            wordcount=(word, count)
            engwords=dict(wordcount)

Recommended Answers

All 8 Replies

def engdicdefine():
    engwords=dict()
    fin = open('words.txt')
    line = fin.readline()
    word = line.strip()
    for word in fin:
        count=len(word)-1
        wordcount=[word, count]
        print wordcount

This prints the words.txt and adds the len but looks like this:

['geotaxis\n', 8]
['geothermal\n', 10]
['geothermic\n', 10]

Guessing this is because fin.readline() gets the \n for next line too? I had to put count=len(word)-1 for it to count the length correctly... now how to get rid of the \n and then add them all to the dictionary?

If the words in your list/file are unique, you can do something like this:

import pprint

# write a test file with unique words
names = '''\
paul
peter
sally
frank
jim
Sandra
quito
'''

fname = "names.txt"
with open(fname, 'w') as fout:
    fout.write(names)

name_dict = {}
# read in the names line by line
for line in open(fname):
    word = line.strip()
    #print(word)
    name_dict[word] = len(word)

pprint.pprint(name_dict)

''' my output -->
{'Sandra': 6,
 'frank': 5,
 'jim': 3,
 'paul': 4,
 'peter': 5,
 'quito': 5,
 'sally': 5}
'''

line 5 should be inside the loop

A modification of Henri's approach using dictionary comprehension ...

import pprint

# write a test file with unique words
names = '''\
Paul
Peter
Sally
Frank
Jim
Sandra
Quasimo
'''

fname = "names.txt"
with open(fname, 'w') as fout:
    fout.write(names)

with open(fname) as fin:
    name_dict = {name.strip():None for name in fin}

pprint.pprint(name_dict)

''' result ...
{'Frank': None,
 'Jim': None,
 'Paul': None,
 'Peter': None,
 'Quasimo': None,
 'Sally': None,
 'Sandra': None}
'''

# testing ...
name = 'Peter'
if name in name_dict:
    print("--> {} found".format(name))

The with statement closes your files safely, and dictionary comprehension is darn easy to comprehent.

I am using Python 2.7. Is there a difference in the syntax? Doesn't appear to work..

The words.txt file is already created and is a list of 110K words. How do I point it in that direction instead of something I have to create above the function?

Also, does the with statement serve as a function? The exercise says to create a function that does this.

Thanks.

No you must move the code inside function yourself.

The with statement was introduced with Python 2.5 and the dictionary and set comprehensions with Python 2.7.3

def dec_build(s):
    my_file = open(s,"r")
    my_dict={}
    for line in my_file:
        for word in line.split():
            my_dict[word]=len(word)

    my_file.close()
    return my_dict

#it will open specified file,read line by line #,word by word
#and store in a dictionary

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.