Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 12:53
Show Gist options
  • Save MichelleDalalJian/3d9930cbc6d23e80cb74a2d37303823a to your computer and use it in GitHub Desktop.
Save MichelleDalalJian/3d9930cbc6d23e80cb74a2d37303823a 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…
fhand = open("romeo.txt")
lst = list()
for line in fhand:
line = line.rstrip()
line = line.split()
for i in line:
if i not in lst:
lst.append(i)
lst.sort()
print(lst)
@Philineza
Copy link

fname = input("Enter file name: ")
fh = open(fname)
lst = list()

for line in fh:
line = line.rstrip()
line = line.split()
for word in line:
if word not in lst:
lst.append(word)
lst.sort()

print(lst)

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