0

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. I am unsure what I need to add to make it work like the comprehension does.

Wiggs
  • 51
  • 1
  • 2
  • 10
  • could probably use this answer, unless you care about number of letters, http://stackoverflow.com/questions/9443302/search-strings-in-list-containing-specific-letters-in-random-order – Albert Rothman Oct 04 '16 at 22:35

1 Answers1

2

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

BPL
  • 9,632
  • 9
  • 59
  • 117
  • That believe that makes a lot more sense. So the comprehension is using the expression(letter in word) then the for loop (word in myList) all within the for loop(letter in mystring) right? I'll certainly read up on the nested comprehension lists. I was just confused on how the list comprehension itself worked when decontructed. Thank you. :) – Wiggs Oct 04 '16 at 22:49
  • @BPL if I may ask, what do the res1 = [] and res2 = [] do? Are they placeholders to check for the characters? – Wiggs Oct 04 '16 at 22:56
  • @Wiggs They are [lists](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists), open an interactive session and try `res1 = []`, then just `type(res1)` or `res1.__class__` . In any case, I'd recommend you go through a python tutorial to get familiar with the python basic blocks ;-) – BPL Oct 04 '16 at 23:06
  • oh ok, thank you again for the example and breakdown, it really helps. – Wiggs Oct 04 '16 at 23:08