练习题:“给出单词列表和文本文件,拼写检查 文本文件的内容并打印所有(唯一)单词 在单词列表中找到。“
我没有得到问题的解决方案所以有人可以告诉我我是怎么去的,应该是什么样的正确答案?:
作为免责声明,我的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
哦,是的...我非常肯定我已经正确地缩进了所有内容,但我不知道如何在这里格式化我的问题,以便它不会像它一样出现。遗憾。
您怎么看?
答案 0 :(得分: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
答案 1 :(得分:0)
我不知道你想要迭代什么,但为什么不首先迭代你的单词(我猜这是变量?)然后对你迭代的每个单词进行迭代wordlist并检查该单词是否在单词列表中。
我不会粘贴代码,因为它似乎是我的家庭作业(如果是这样,请添加家庭作业标签)。
顺便说一下open()的第一个参数应该是一个字符串。
答案 2 :(得分:0)
真的很简单。将两个列表转为sets,然后转到difference。应该采取10行代码。你只需要自己弄清楚语法;)你不会通过让我们为你编写来学习任何东西。