【发布时间】:2016-06-30 08:58:06
【问题描述】:
问题: 打开文件 romeo.txt 并逐行读取。对于每一行,使用 split() 方法将该行拆分为一个单词列表。该程序应该建立一个单词列表。对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。程序完成后,按字母顺序对生成的单词进行排序和打印。
fname = raw_input('Enter the file name: ')
file = open(fname)
romeo = list()
for line in file:
words = line.split()
for current_word in words :
if current_word in romeo:
continue
romeo.append(current_word)
print romeo.sort()
我得到“无”的输出。我不确定我做错了什么 This link is where the romeo.txt file is found.
【问题讨论】:
标签: python list file split append