[Tutor] Iteration issues

boB Stepp robertvstepp at gmail.com
Thu May 10 09:18:44 EDT 2018


Greetings!

On Wed, May 9, 2018 at 6:27 PM, Roger Lea Scherer <rls4jc at gmail.com> wrote:
> 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()

A small quibble:  Unless you get lucky, the identifier 'sentence' is
unlikely to be an actual sentence.

>         sentence = sentence.strip(string.punctuation)

This will only remove punctuation at the beginning or the end of the line.

>         print(sentence)
>         sentence = sentence.lower()
>         word_list = sentence.strip()

Haven't you already done this above?

>         word_list = word_list.split(' ')
>
>         for i in range(len(word_list)):

It is better style and more direct to write this as

for word in word_list:

If you need the index of the word in the word_list (as you do below)
then you can use enumerate:

for index, word in enumerate(word_list):

>             word_list[i] = word_list[i].strip(string.punctuation)
>         print(word_list)
>
>         if word_list[i] in wordset:
>             wordset[word_list[i]] += 1
>         else:
>             wordset[word_list[i]] = 1
>         print(wordset)

And here we come to the answer to the actual question you asked.  Look
at your indentation.  Your if/else construct is not within your for
loop's scope/block, so you are only checking for the last value of i,
which corresponds to the last word in word_list.  So, as you have
written your code:

with ...
    for line in f:
        ...
        for i in range(len(word_list)):
            ...
            if word_list[i] = ...
                wordset[word_list[i]] += 1
            else:
                wordset[word_list[i]] = 1

I used "..." to replace much of your code so you can see the needed
indentation levels better.

HTH!

-- 
boB


More information about the Tutor mailing list