suppose I have the following text file:
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
I want to add all unique words in this file to a list
fname = open("romeo.txt")
lst = list()
for line in fname:
line = line.rstrip()
words = line.split(' ')
for word in words:
if word in lst: continue
lst = lst + words
lst.sort()
print lst
but the optupt of the program is as follows:
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and',
'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief',
'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick',
'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what',
'window', 'with', 'yonder']
'and' and a few other words appear multiple times in the list. Which part of the loop should I change, so that I don't have any duplicate words? Thanks!
Aucun commentaire:
Enregistrer un commentaire