I need to count all unique five letter words in a txt file and ignore any word with an apostrophe. I'm new to python so I am quite confused trying to get just the five letter words and not sure how to ignore words that have an ' . what I wrote so far seemed to work for filtering the unique words but not for just five letter words.
with open ("names.txt", 'r') as f: #open the file
words = f.read().lower().split() #read the contents into a sting, made all the character in string lower case and split string into list of words
print(words)
unique_words = set(words) #get unique words
print(len(unique_words))
for w in unique_words:
if len(w) == 5:
print(unique_words)
else:
pass
CodePudding user response:
Your code looks good. I think the only bit you did wrong was to print(unique_words)
instead of print(w)
when you found a word w
of length 5.
To ignore the words containing '
you can add this condition:
for w in unique_words:
if len(w) == 5 and "'" not in w:
print(w)
B.t.w. you don't need the pass
statement if you are already at the end of the for loop.
CodePudding user response:
This should do the trick
with open("names.txt", 'r') as f: #open the file
words = f.read().lower().split() #read the contents into a sting, made all the character in string lower case and split string into list of words
print(words)
unique_words = set() #Create empty set
for w in words:
if len(w) == 5 and "'" not in w:
unique_words.add(w) #add words to set
print(len(unique_words))