简体   繁体   English

如何在Python中分解此列表理解?

[英]How do I break down this list comprehension in Python?

I seen a question earlier on how to find the characters to a specific word from a list of strings. 我之前看到一个问题,该问题是如何从字符串列表中找到特定单词的字符。 It got deleted I think because I can't find it anymore. 我认为它已删除,因为我找不到了。

So for example: 因此,例如:

>>>findTheLetters(["hello", "world"], "hold")
>>>True
>>>findTheLetters(["hello", "world"], "holn")
>>>False (because of no "n")

So I seen a post by someone on here saying to use list comprehension like so: 因此,我在这里看到某人的帖子,说要像这样使用列表理解:

return all((any(letter in word for word in myList)) for letter in myString)

my question is, is how would I break down that list comprehension so I can understand how it works? 我的问题是,我该如何分解列表理解,以便我能理解它的工作原理? I've used simple(newbie) list comprehension but nothing like that. 我已经使用了简单的(新手)列表理解功能,但是没有类似的方法。

My attempt: 我的尝试:

def findTheLetters(myList, myString):
    for word in myList:
        for letter in word:
            #something goes here?
        return letter in myString

That is the farthest I've gotten. 那是我走的最远。 It works sometimes like with "lord" and "hold", but like if I try "hell" or "woe" for example it still gives me false even though the characters "h" "e" "l" "l" and "w" "o" "e" are in the list of words. 有时它与“ lord”和“ hold”类似,但是就像我尝试“ hell”或“ woe”一样,即使字符“ h”,“ e”,“ l”,“ l”和“ w“” o“” e“在单词列表中。 I am unsure what I need to add to make it work like the comprehension does. 我不确定我需要添加什么以使其像理解一样工作。

Here's a little educative example to show you what that algorithm is doing behind the curtains: 这是一个教育性的示例,向您展示该算法的幕后工作:

def findTheLetters(myList, myString):
    return all((any(letter in word for word in myList)) for letter in myString)


def findTheLetters1(myList, myString):
    res1 = []
    for letter in myString:
        res2 = []
        for word in myList:
            res2.append(letter in word)

        print(letter, res2, any(res2))

        res1.append(any(res2))

    print('-' * 80)
    print(res1, all(res1))
    print('-' * 80)
    return all(res1)

findTheLetters1(["hello", "world"], "hold")
findTheLetters1(["hello", "world"], "holn")

Output: 输出:

h [True, False] True
o [True, True] True
l [True, True] True
d [False, True] True
--------------------------------------------------------------------------------
[True, True, True, True] True
--------------------------------------------------------------------------------
h [True, False] True
o [True, True] True
l [True, True] True
n [False, False] False
--------------------------------------------------------------------------------
[True, True, True, False] False
--------------------------------------------------------------------------------

I'd recommend you learn & read a about any/all operators and also about nested comprehension lists to know the order of execution. 我建议您学习并阅读有关任何/所有运算符以及嵌套理解列表的知识,以了解执行顺序。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM