当我运行以下代码时,出现“主循环'builtin_function_or_method'对象不可迭代”错误。错误发生在 对于search_list中的word_search:
我不知道什么是内置函数或方法。
end_search=[]
#Remove punctuation
for word_search in search_list:
#Find punctuation
if word_search.find('"')>0:
word_search=word_search.replace('"','')
if word_search.find("'")>0:
word_search=word_search.replace("'",'')
end_search.append(word_search)
这是读取文本文件并返回出现在不同文本文件中的单词的程序的一部分
答案 0 :(得分:0)
因此,我只运行了您提供的代码,如下所示:
search_list = ["hi ' my ' name ' is ' jeremiah", 'this "is " a " list " of " strings']
end_search=[]
#Remove punctuation
for word_search in search_list:
#Find punctuation
if word_search.find('"')>0:
word_search=word_search.replace('"','')
if word_search.find("'")>0:
word_search=word_search.replace("'",'')
end_search.append(word_search)
print(end_search)
工作正常。
>>>['hi my name is jeremiah', 'this is a list of strings']
这使我相信问题出在search_list
中。您是在尝试对其进行迭代之前将其制作为实际列表,还是试图对称为search_list
的函数或方法进行迭代?如果是后一种情况,那么如果您遍历search_list()
,它仍然可能起作用,但是最好还是明确创建一个要遍历的列表,以确保避免出现时髦的毛刺。