在python中将yield与if/else循环结合起来

我想在法语单词列表中加入由星号 (*) 分隔的两个单词。加入这些词后,我想检查这个词是否存在于法语词典中。如果是,则连接的单词应保留在列表中,否则应附加到另一个列表中。我在我的代码中使用了 yield(我是这个函数的新手),但是我的嵌套 if/else 循环有问题。任何人都可以帮助我实现我的目标吗?我失败的代码如下:


words = ['Bien', '*', 'venue', 'pour', 'les','engage', '*', 'ment','trop', 'de', 'YIELD', 'peut','être','contre', '*', 'productif' ]


with open ('Fr-dictionary.txt') as fr:

    dic = word_tokenize(fr.read().lower())


l=[ ]


def join_asterisk(ary):

    i, size = 0, len(ary)

    while i < size-2:

        if ary[i+1] == '*':

            if ary[i] + ary[i+2] in dic:

                yield ary[i] + ary[i+2]

                i+=2

            else: yield ary[i]

            i+=1

            l.append(ary[i] + ary[i+2])

    if i < size:

        yield ary[i]




print(list(join_asterisk(words)))


慕莱坞森
浏览 358回答 3
3回答

隔江千里

生成器非常适合这个用例,您可以将生成器视为一个函数,它将一个一个地而不是一次(如 return 那样)为您提供产生的值。换句话说,您可以将它视为一个不在内存中的列表,一个只有在被要求时才能获取下一个元素的列表。另请注意,生成器只是构建迭代器的一种方式。在您的情况下,这意味着您不必构建一个列表l来跟踪正确的单词,因为生成器join_asterisk将为您生成正确的单词。您需要做的是迭代此生成器将产生的所有值。这正是list(generator)要做的,它将通过迭代生成器的所有值来构建一个列表。最后,代码将如下所示:# That look better to me (just in case you change it later)word_separator = '*'words = ['Bien', word_separator, 'venue', 'pour', 'les','engage', word_separator, 'ment','trop', 'de', 'YIELD', 'peut', word_separator, "tard"]# Fake dictionarydic = {"Bienvenue", "pour", "les", "engagement", "trop", "de", "peut", "peut-être"}def join_asterisk(ary):&nbsp; &nbsp;for w1, w2, w3 in zip(words, words[1:], words[2:]):&nbsp; &nbsp; &nbsp; if w2 == word_separator:&nbsp; &nbsp; &nbsp; &nbsp; word = w1 + w3&nbsp; &nbsp; &nbsp; &nbsp; yield (word, word in dic)&nbsp; &nbsp; &nbsp; elif w1 != word_separator and w1 in dic:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yield (w1, True)correct_words = []incorrect_words = []for word, is_correct in join_asterisk(words):&nbsp; if is_correct:&nbsp; &nbsp; correct_words.append(word)&nbsp; else:&nbsp; &nbsp; incorrect_words.append(word)print(correct_words)print(incorrect_words)这输出['Bienvenue', 'pour', 'les', 'engagement', 'trop', 'de']['peuttard']另请注意,您可以使用列表理解而不是使用 for 循环来填充两个列表:correct_words = [w for w, correct in join_asterisk(words) if correct]incorrect_words = [w for w, correct in join_asterisk(words) if not correct]

PIPIONE

似乎是以下几行:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l.append(ary[i]&nbsp;+&nbsp;ary[i+2])缩进不够,因此不涉及else.&nbsp;这意味着每对中间带有 * 的单词都将被附加到,l而不仅仅是不在dic.

MMTTMM

你不是在寻找这样的东西吗:def join_asterisk(ary):i, size = 0, len(ary)while i < size-2:&nbsp; &nbsp; if ary[i+1] == '*':&nbsp; &nbsp; &nbsp; &nbsp; if ary[i] + ary[i+2] in dic:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield ary[i] + ary[i+2]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i+=2&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield ary[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i+=1&nbsp; &nbsp; &nbsp; &nbsp; l.append(ary[i] + ary[i+2])if i < size:&nbsp; &nbsp; yield ary[i]'else' 块遵循相同的规则。例如,在 'if'、'elif'、'else' 或 'while' 子句的同一行中添加一个表达式是可行的,但是如果您想要的不仅仅是与子句相关的表达式,您必须使用缩进或将表达式与';' 像这样:while 1:print(9,end='');print(8)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python