TypeError:'函数'对象不可迭代'Python 3 [英] TypeError: 'function' object is not iterable' Python 3
问题描述
我正在尝试编写一个程序,该程序将从网络上打开一个由10,000个单词组成的文本文件,然后清理该文件以消除无用的单词,例如 aa。我最终希望使用这些单词来做其他事情,因此我想将非无意义单词添加到新列表中。每次我尝试运行此命令时,我都会遇到错误代码 TypeError:函数对象不可迭代
。
I'm trying to write a program that will open a text file from the web consisting of 10,000 words, then 'clean' the file to get rid of nonsense words, such as 'aa'. I eventually want to use these words to do other things so I want to add the non 'non-sense' words to be put into a new list. Every time i try to run this I run into the error code TypeError: 'function' object is not iterable
.
import urllib.request
def readWordList():
response = urllib.request.urlopen("http://www.mit.edu/~ecprice/wordlist.10000")
html = response.read()
data = html.decode('utf-8').split()
return data
clean = readWordList()
def clean(aList):
newList = []
for word in aList:
if range(len(word)) >= 2:
newList.append(word)
return newList
clean(clean)
推荐答案
下定决心:干净
应该是列表还是函数?您从一个列表开始,但是后来用一个函数替换了它,然后告诉函数清理自身。试试这个:
Make up your mind: is clean
supposed to be a list or a function? You started as a list, but then replaced that with a function, and then told the function to clean itself. Try this:
dirty_list = readWordList()
def clean(aList):
...
clean(dirty_list)
这篇关于TypeError:'函数'对象不可迭代'Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!