试图写成一部已经完成任务的法典的I m New to Zhu and I m:
- I need to 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() function. * * Build a list of words as follows:
- For each word on each line check to see if the word is already in the list
- If not append it to the list.
- When the program completes, sort and print the resulting words in alphabetical order.
http://www.pythonlearn.com/code/romeo.txt” rel=“nofollow”http://www.pythonlearn.com/code/romeo.txt 。
这是我迄今为止所做的:
fname = raw_input("Enter file name: ")
if len(fname) == 0:
fname = open( romeo.txt )
newList = []
for line in fname:
words = line.rstrip().split()
print words
我知道,我需要使用另一个<条码>查询条码>,以检查任何缺失的词句,最后,我需要利用<条码>sort(条码>的功能加以分类。 灰色译员给我一个错误说,如果不存在的话,我必须使用<编码>append(<>append(>)。
我设法用我的代码编制以下清单:
[ But , soft , what , light , through , yonder , window , breaks ] ← Mismatch
[ 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 ]
但产出应如此:
[ 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 ]
我如何调整我的法典以产生这一产出?
Important Note: To everyone wants to help, Please make sure that you go from my code to finish this tast as it s an assignment and we have to follow the level of the course. Thanks
这是我对法典的更新:
fname = raw_input("Enter file name: ")
if len(fname) == 0:
fname = open( romeo.txt )
newList = list()
for line in fname:
words = line.rstrip().split()
for i in words:
newList.append(i)
newList.sort()
print newList
[ 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 ]
But I m getting duplication! Why is that and how to avoide that?