Remove fullstop, commas, quotation from list in Python

sulav_lfc picture sulav_lfc · Feb 19, 2014 · Viewed 11.1k times · Source

I have a python code for word frequency count from a text file. The problem with the program is that it takes fullstop into account hence altering the count. For counting word i've used a sorted list of words. I tried to remove the fullstop using

 words = open(f, 'r').read().lower().split()  
 uniqueword = sorted(set(words))
 uniqueword = uniqueword.replace(".","") 

but i get error as

AttributeError: 'list' object has no attribute 'replace'

Any help would be appreciated :)

Answer

jonrsharpe picture jonrsharpe · Feb 19, 2014

You can process the words before you make the set, using a list comprehension:

words = [word.replace(".", "") for word in words]

You could also remove them after (uniquewords = [word.replace...]), but then you will reintroduce duplicates.

Note that if you want to count these words, a Counter may be more useful:

from collections import Counter

counts = Counter(words)