[Tutor] Iteration issues

Mats Wichmann mats at wichmann.us
Thu May 10 09:20:47 EDT 2018


On 05/09/2018 05:27 PM, Roger Lea Scherer 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()
>         sentence = sentence.strip(string.punctuation)
>         print(sentence)
>         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)
# notice the following lines are not inside the loop, as seen
# by the indentation. Thus the value of i will be the value at
# the end of the loop. Exactly "the last word in the line".
>         print(word_list)
> 
>         if word_list[i] in wordset:
>             wordset[word_list[i]] += 1
>         else:
>             wordset[word_list[i]] = 1
>         print(wordset)
> 
> 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.
> 



More information about the Tutor mailing list