Skip to content

Instantly share code, notes, and snippets.

@hareom284
Created August 10, 2020 17:56
Show Gist options
  • Save hareom284/2f7238f237808ae3243caa2fc90ba603 to your computer and use it in GitHub Desktop.
Save hareom284/2f7238f237808ae3243caa2fc90ba603 to your computer and use it in GitHub Desktop.
8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in…
fname = input("Enter file name: ")
try:
fh = open(fname)
lst = list()
for line in fh:
lst.append( line.split())
newlist = sum(lst,[])
newlist.sort()
print([newlist[i] for i in range(len(newlist)) if i == newlist.index(newlist[i])])
except :
print(fname,"file is does not exit")
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment