gpt4 book ai didi

python - 类型错误 : object of type 'NoneType' has no len() while iterating through a list

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:29 33 4
gpt4 key购买 nike

对于下面的程序,我收到以下错误“TypeError:类型为‘NoneType’的对象没有 len()”。为什么我不能迭代一个列表并将其与另一个列表进行比较?

word_list = list() 

while True:
file_name = raw_input('Enter file name: ')
if len(file_name) < 1: exit()
try:
file = open(file_name)
break

except:
print 'Please enter a valid file name.'
continue

for line in file:
line = line.rstrip()
words = line.split()
for word in words:
if len(word_list) <1:
word_list = word_list.append(word)

else:
if not word in word_list:
word_list = word_list.append(word)

word_list = word_list.sort()
print word_list

最佳答案

list.append返回

以下行:

word_list = word_list.append(word)

应替换为:

word_list.append(word)

否则,word_list 会变为 None,导致稍后出现 TypeError

关于python - 类型错误 : object of type 'NoneType' has no len() while iterating through a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248748/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com