Programming for Everybody: Assignment 08.4 Lists - edorlando07/datasciencecoursera GitHub Wiki

###Python Data Structures

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.pythonlearn.com/code/romeo.txt

The entire text file is listed below:

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

The actual code is listed below:

fname = raw_input("Enter file name: ")
fh = open(fname)
FinalList = list()  

Goes through each line in fh

for line in fh:

    for word in line.split():        ##Goes through through each word on line    
        if not word in FinalList:    ##Checks to see if word is already in list
            FinalList.append(word)   ##Concatenates words to list
    
FinalList.sort()
print FinalList

The input/output of the program is listed below:

Enter file name: romeo.txt
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill',    
'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']