【发布时间】:2016-04-06 15:21:27
【问题描述】:
我想建立一个单词列表。对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。程序完成后,按字母顺序对生成的单词进行排序和打印。 但是当我向列表中添加一个字符串时,它显示“'NoneType' 类型的参数不可迭代”。担心什么?
fh = ("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")
lst = list()
for line in fh:
words = line.split()
for word in line:
if word not in lst:
lst = lst.append(word)
lst.sort()
print lst
【问题讨论】:
-
1) 尝试打印
fh;这不是你想的那样。 2) 为什么你做words = line.split()然后对words什么都不做?for word in line:应该是for word in words:吗? 3) 你希望lst = lst.append(word)做什么? 4)为了有效地做到这一点,你应该使用set。
标签: python list sorting iterable nonetype