【发布时间】:2014-07-28 10:26:36
【问题描述】:
我正在尝试编写一个 python 代码,它从文本文件中读取一堆行,然后将这些行拆分为单词。然后对于每个单词,它检查单词是否已经存在于列表中,如果不存在则添加到它,最后对列表进行排序并打印最终列表。这是我目前写的。
fname = raw_input("Enter file name: ")
fh = open(fname)
new = list()
for line in fh:
line = line.rstrip()
word = line.split()
for w in word:
if w not in new:
final = new.append(w)
result = final.sort()
print result
但我收到以下错误..
AttributeError: 'NoneType' object has no attribute 'sort' on line 12
不知道为什么?有什么帮助吗?
谢谢 乌彭德拉
【问题讨论】:
-
.append不返回任何内容。 -
你只需要
new.append(...)。你不需要final = new.append(...)。然后简单排序new