Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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/6/EmptyTag/133.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,因此,我希望打印出一个样本文件中拼写错误的单词,我似乎不知道如何将两个列表循环在一起。代码如下:(顶部只是一个helper函数),dict.txt只是每个可能单词的一个长列表 def cleanWords(wlist): ret=[] for word in wlist: cleanword = word.strip('?.,;:!\'-"\n()') ret.append(cleanword.lower()) return ret f

因此,我希望打印出一个样本文件中拼写错误的单词,我似乎不知道如何将两个列表循环在一起。代码如下:(顶部只是一个helper函数),dict.txt只是每个可能单词的一个长列表

def cleanWords(wlist):
    ret=[]
    for word in wlist:
        cleanword = word.strip('?.,;:!\'-"\n()')
        ret.append(cleanword.lower())
    return ret

f = open('dict.txt', 'r')
lines = f.readlines()
cleanlistdic = cleanWords(lines)

inword = raw_input("Enter a file name: ")
g = open(inword, 'r')
lines2 = g.readlines()
cleanlistfile = cleanWords(lines2)

# this part is where i get stuck
for line in cleanlistfile:
    for j in cleanlistdic:
        if line not in cleanlistdic:
            print line

您的内部循环是毫无意义的(您从未使用过
j


您只需要一个循环:对于每个
,如果它不在
cleanlistdic
中,请打印它。您可以使用列表理解来完成此任务:

desired_lines = [line for line in cleanlistfile if line not in cleanlistdic]
如果要打印每行,可以使用for循环:

for line in desired_lines:
     print line

现在它只是打印测试文件中的所有单词。有什么建议吗?对于cleanlistfile中的行:if(不在cleanlistdict中的行):打印行