[Tutor] Fwd: Help/suggestion requested

Alan Gauld alan.gauld at yahoo.co.uk
Tue Apr 12 03:48:16 EDT 2022


On 12/04/2022 08:19, Alan Gauld wrote:

> code, I have managed to get the desired result. 

Well done!

> Please see the amended code below and if there is room for improvement,

There is always room for improvement! :-)

> #create a dictionary to store the words and their frequencies as key :
> value pair.
> word_dictionary = {}
> 
> #split and store the sample text in a list
> text_list = text.split()
> print(text_list)
> 
> #new list to store words without the common words
> new_list = []
> 
> #define unwanted characters as string
> special_characters = '''.,/?@:;{[]_ }'"-+=!£$%^&*()~<>¬`'''
> 
> #define less desired or common words as a list
> less_desired_words = ['the', 'a', 'they', 'are', 'i', 'me', 'you', 'we',
> 'there', 'their', 'can', 'our', 'is', 'not', 
> 'be','for', 'in', 'on', 'no', 'have', 'he', 'she', 'and', 'your', 'him',
> 'her', 'at', 'of', 'that','his', 'what', 'it','where', 'will']
> 
> #iterate through text_list and remove the punctuations and convert to
> lower case words and add to new list       
> for word in text_list:
>     for character in special_characters:
>         word = word.lower()   # move this outside the loop so it only gets done once
>         word = word.replace(character, "")

      if word not in less_common_words:

>           new_list.append(word)

Instead of adding to the list add to a dictionary with the count.
Use dict.get() which looks like:

############
words = {}

...

words[word] = words.get(word,0) + 1
############

get(words,0) returns zero if words is not in the dict.

> 
> print(new_list)    
>   

The loop below is no longer necessary...

> #iterate through new list and remove the common words
> for word in new_list:
>     for common_word in less_desired_words:
>         if common_word in new_list:
>             new_list.remove(common_word)       
> 
> print(new_list)
> 

Counting the words is no longer necessary

> #count the words in the list and add to dictionary with their frequecy
> as key:value pair            
> for word in new_list:
>     if word in word_dictionary:
>         frequency = word_dictionary[word]
>         word_dictionary[word] = frequency + 1
>         
>     else:
>         word_dictionary[word] = 1
> 
> print(word_dictionary)-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list