[Tutor] Iteration issues

Roger Lea Scherer rls4jc at gmail.com
Wed May 9 19:27:59 EDT 2018

Hello, again.

I want to count words in a text file. If a word repeats I want to increase
the count by 1; if the word is new to the dictionary, I want to add the
word to the dictionary. Everything works like I would like and expect,
except for it only adds the last word of each line to the dictionary. What
am I missing?

import string

file_name = 'oxford.txt'
wordset = {}
with open(file_name, 'r') as f:
    for line in f:
        sentence = line.strip()
        sentence = sentence.strip(string.punctuation)
        sentence = sentence.lower()
        word_list = sentence.strip()
        word_list = word_list.split(' ')

        for i in range(len(word_list)):
            word_list[i] = word_list[i].strip(string.punctuation)

        if word_list[i] in wordset:
            wordset[word_list[i]] += 1
            wordset[word_list[i]] = 1

The output is: (I included only the first four lines)

The Project Gutenberg EBook of Advice to a Young Man upon First Going to
['the', 'project', 'gutenberg', 'ebook', 'of', 'advice', 'to', 'a',
'young', 'man', 'upon', 'first', 'going', 'to']
{'to': 1}
Oxford, by Edward Berens
['oxford', 'by', 'edward', 'berens']
{'to': 1, 'berens': 1}

{'to': 1, 'berens': 1, '': 1}
This eBook is for the use of anyone anywhere at no cost and with
['this', 'ebook', 'is', 'for', 'the', 'use', 'of', 'anyone', 'anywhere',
'at', 'no', 'cost', 'and', 'with']
{'to': 1, 'berens': 1, '': 1, 'with': 1}

Thank you as always.

Roger Lea Scherer

More information about the Tutor mailing list