In: Computer Science
Write a python program that does the
following:
Prompt for a file name of text words.
Words can be on many lines with multiple words per line.
Read the file and convert the words to a list.
Call a function you created called list_to_once_words(), that takes
a list as an argument and returns a
list that contains only words that occurred once
in the file.
Print the results of the function with an appropriate
description.
Think about everything you must do when working with a
file.
No Example Output provided for
this question.
def list_to_once_words(list):
once_words=[]#empty list
for i in range(0,len(list)):
temp=0
for j in
range(0,len(list)):
#checking the condition with case sensitive
if(list[i].lower()==list[j].lower() and i!=j):
temp=1
break
if(temp==0):
#appending to the list
once_words.append(list[i])
return once_words#returning the list
#taking filename from the user
file_name=input("Enter the name of the file:")
list=[]
# opening the text file
with open(file_name,'r') as file:
# reading each line
for line in file:
# reading each
word
for word in
line.split():
# appending the
words
list.append(word)
print("original list:",list)#
result=list_to_once_words(list)
print("The list with only words that occurred once in the
file:",result)
name.txt file is above