Skip to content

Instantly share code, notes, and snippets.

@ayubmetah
Created December 21, 2020 01:07
Show Gist options
  • Save ayubmetah/4557f4e6cbc8bdd26018ae5c1392ceaa to your computer and use it in GitHub Desktop.
Save ayubmetah/4557f4e6cbc8bdd26018ae5c1392ceaa 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: ")
fh = open(fname, 'r')
lst = list()
for line in fh:
words = line.split()
for word in words:
if word in lst: continue
lst.append(word)
lst.sort()
print(lst)
@ayubmetah
Copy link
Author

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 alphabetical order.
You can download the sample data at http://www.py4e.com/code3/romeo.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment