Python中的拼写检查程序

-2 投票
3 回答
3602 浏览
提问于 2025-04-17 01:33

练习题目:“给定一个单词列表和一个文本文件,检查文本文件的内容,打印出所有(唯一的)在单词列表中找不到的单词。”

我没有找到这个问题的解决方案,能不能告诉我我该怎么做,以及正确的答案应该是什么?

需要说明的是,这些内容在我的Python控制台中都无法运行……

这是我尝试的代码:


a=list[....,.....,....,whatever goes here,...]

data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()          

#I'm aware that something is wrong here since I get an error when I use it.....when I just write blablabla.txt it says that it can't find the thing. Is this function only gonna work if I'm working off the online IVLE program where all those files are automatically linked to the console or how would I do things from python without logging into the online IVLE?

    for words in data:

        for words not in a

            print words

wrong = words not in a

right = words in a

print="wrong spelling:" + "properly splled words:" + right

哦,对了……我很确定我所有的缩进都没问题,但我不知道怎么在这里格式化我的问题,所以它才会像现在这样显示,抱歉。

你觉得怎么样?

3 个回答

0

其实很简单。把两个列表都变成集合,然后找出它们的差异。这大概只需要10行代码。你只需要自己搞清楚语法就行了 ;) 如果我们帮你写出来,你就学不到什么了。

0

我不太清楚你想要遍历什么,但你可以先遍历你的单词(我猜这些单词在变量 a 里?),然后对于 a 中的每一个单词,再去遍历单词列表,检查这个单词是否在单词列表里。

我不打算贴代码,因为这看起来像是作业(如果真的是作业,请加上作业标签)。

顺便说一下,open() 函数的第一个参数应该是一个字符串。

2

这段代码有很多问题——我会在下面指出一些,但我强烈建议你去了解一下Python的控制流结构、比较运算符和内置数据类型。

a=list[....,.....,....,whatever goes here,...]

data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()          
# The filename needs to be a string value - put "C:\..." in quotes!

for words in data:
# data is a string - iterating over it will give you one letter
# per iteration, not one word

    for words not in a
    # aside from syntax (remember the colons!), remember what for means - it
    # executes its body once for every item in a collection. "not in a" is not a
    # collection of any kind!

        print words

wrong = words not in a
# this does not say what you think it says - "not in" is an operator which
# takes an arbitrary value on the left, and some collection on the right,
# and returns a single boolean value

right = words in a
# same as the previous line

print="wrong spelling:" + "properly splled words:" + right

撰写回答