Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';在<;字符串>';需要字符串作为左操作数,而不是列表_Python - Fatal编程技术网

Python ';在<;字符串>';需要字符串作为左操作数,而不是列表

Python ';在<;字符串>';需要字符串作为左操作数,而不是列表,python,Python,我正在学习开发一个应用程序,从.txt文件中输入单词,并检查字典文件中是否存在单词 当我运行代码时,我得到一个类型错误。请帮帮我,我做错了什么 input.txt文件包含段落句子。 dictionary.txt文件逐行列出单词列表 def word_check(check_file, input_file): try: open_file = open(check_file, "r") read_file = open_file.readlines()

我正在学习开发一个应用程序,从
.txt
文件中输入单词,并检查字典文件中是否存在单词

当我运行代码时,我得到一个类型错误。请帮帮我,我做错了什么

input.txt
文件包含段落句子。
dictionary.txt
文件逐行列出单词列表

def word_check(check_file, input_file):
    try:
        open_file = open(check_file, "r")
        read_file = open_file.readlines()

        open_file_2 = open(input_file, "r")
        read_file_2 = open_file_2.readlines()

        for input_word in read_file_2:
            input_word = input_word.strip("!@#$%^&*()_+{}:?><'-=,./;][")
            each_input_word = input_word.lower().split()
            list_each_word = each_input_word
            count = 0

            for item in read_file:
                line = item
                for word in line:
                    check_word = word.lower()
                    if list_each_word in check_word:
                        count += 1
                print(count)

    except FileExistsError:
        print("File not exist")


word_check("list.txt", "input.txt")
def word\u检查(检查文件、输入文件):
尝试:
打开文件=打开(选中文件“r”)
read_file=打开_file.readlines()
打开\u文件\u 2=打开(输入\u文件,“r”)
read_file_2=打开_file_2.readlines()
对于读取文件2中的输入字:

input_word=input_word.strip(“!@$%^&*()_+{}:?>”通过执行
input_word.lower().split()
生成
列出每个_word

这将生成一个单词列表

然后,在后面的单词列表中循环,如果在check\u word:
中列出每个单词,则执行

在这里,
check\u word
是一个字符串,
list\u每个单词
都是一个列表。您应该切换这些,因为您要检查字符串是否在列表中

因此,它应该是:


如果在列表中检查每个单词:

如果您试图在
列表中计算每个单词出现的次数,您可以替换这两行:

if list_each_word in check_word:
    count += 1
为此:

count += list_each_word.count(check_word)
它将查找每个单词在
列表中出现的次数


我知道了

    def spell_check(dictionary, document):
    try:
    open_document = open(document, "r")
    input_sentence = open_document.readline()
    input_words = input_sentence.lower().split()
    print(input_words)

    open_dictionary = open(dictionary, "r")
    check_sentence = open_dictionary.read()
    check_word = check_sentence.lower()

    for word in input_words:
        word = word.strip("!@#$%^&*()_+{}:?><,./;[]=-")
        if word not in check_word:
            print(f"Mispelled words are: {word}")

    except FileExistsError:
    print("File does not exist")


    spell_check("dictionary.txt", "document.txt")
def拼写检查(字典、文档):
尝试:
打开文件=打开(文件“r”)
input\u句子=打开\u文档。readline()
输入单词=输入句子。lower().split()
打印(输入单词)
open_dictionary=open(dictionary,“r”)
检查句子=打开字典。阅读()
检查单词=检查句子
对于输入单词中的单词:

word=word.strip(“!@$%^&*()”{}:?>如果列表中的每个单词都在检查单词:
中,那么读取
的行应该是
如果列表中的每个单词都在检查单词:
。您得到的错误是,您需要检查字符串是否在列表中,而不是列表是否在字符串中。请指出错误指向的代码行。该信息应该包含在错误中消息。我假设您的标题是实际的错误消息?如果是,它会准确地告诉您出了什么问题:您在
表达式中有一个
…要求
中的
左侧为字符串,但您没有这样做。因此这是一个错误。请始终记住关闭您打开的文件。@Ahndwoo当我在列表中选中单词时ach_word:这将运行检查字典文件中的单词是否存在于输入文件中。我想做相反的操作。我想检查输入文件中的单词是否存在于字典文件中。关于如何将每个单词的列表逐字传递到字符串中以检查字典文件中是否存在单词,我陷入了困境(检查文件)。谢谢!@TeneshVignesan好的,那么你的目标是查看每个单词在
列表中出现多少次
检查单词
?当我在列表中检查单词时,每个单词:这将运行检查字典文件中的单词是否存在于输入文件中。我想做相反的操作。我想检查输入文件中的单词是否要检查d在字典文件中。如何将每个单词的列表逐字地传递到字符串中,以便检查字典文件中是否存在这些单词(检查文件),我陷入了困境。谢谢!我想我把你弄糊涂了。首先,我想从txt文件中检查一个段落的拼写。我想将段落中的每个单词与一个字典txt文件进行比较。段落文件=输入文件。字典文件=检查文件。我只想计算字典文件中存在的段落中的单词。如目前,我一直在研究如何将段落中的每个单词作为字符串逐字传递。但是,当我运行代码时,它会一个字母一个字母地传递。我希望我已经讲清楚了。对于给您带来的不便,我深表歉意@Ahndwoo@TeneshVignesan好吧,“我有这个错误”和“我希望我的代码能够做到这一点,但我不知道如何做到"。您的问题询问了一个错误,我们中的一些人在评论中对此进行了回答。这个答案可能不是您想要的答案,但现在您知道了错误的原因,可以尝试解决它。如果您仍然被卡住,我建议专门问一个关于该问题的新问题。我明白了。很抱歉,我是stackoverflow的新手。我会先尝试解决这个问题,然后发布新问题。谢谢!我已经找到了答案。谢谢!@Ahndwoo