这段代码的哪一部分不可迭代?

问题描述 投票:-3回答:1

当我运行下面的代码时,我得到“主循环'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)

这是读取文本文件并返回在不同文本文件中给出的单词出现的程序的一部分

python built-in iterable
1个回答
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()进行迭代它仍然可以工作,但你最好明确地创建一个列表来迭代,以确保你避免时髦的故障。

© www.soinside.com 2019 - 2024. All rights reserved.