[Tutor] Iteration issues

boB Stepp robertvstepp at gmail.com
Thu May 10 09:49:37 EDT 2018

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()
>         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)

I was wondering if you might want to write a small function so that
you can remove all punctuation symbols from each line in one fell
swoop?  Something like (in pseudocode):

Iterate over string.punctuation.
If a punctuation symbol is in your string:  Replace that symbol with
an empty string.

That might make your code more direct and compact.


More information about the Tutor mailing list