我有一个文本文件:
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
说明:打开文件,逐行阅读。对于每一行,使用 split() 方法将该行拆分为单词列表。该程序应该建立一个单词列表。对于每一行的每个单词,检查该单词是否已经在列表中,如果不在列表中,则将其添加到列表中。程序完成后,按字母顺序排序并打印生成的单词。
期望的输出:
['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']
我被困在这里:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line=line.rstrip()
lst = line.split()
lst.append(line)
lst.sort()
print(lst)
守着一只汪
隔江千里
HUH函数
相关分类