Python data structure: Assignment 8.4

Coursera: Python data structures
Assignment 8.4
Week 4

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

Solution:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    for i in line.rstrip().split(' '):
        lst.append(i)
lst = list(set(lst))
lst.sort()
print(lst)

Comments

Post a Comment

Comments here for more information.........

Popular posts from this blog

Python data structures: Assignment 7.1

Programming for everybody (Python) Assignment 5.2

Coursera:Web application technologies and Django