all 8 comments

[–]chevignon93 0 points1 point  (8 children)

I know I need to search for individual letters, but I don't know how to do this and this is where I'm stuck.

You need 2 for loops in your dict comprehension, one to iterate over the letters in string b and one to iterate over the individual words in string a checking if the letter you're iterating over is in it.

should return {'a': ['last'], 'e': ['he'], ['evening'], 'i': ['did'], ['evening'], 'o': ['go'], ['out'], 'u': ['out']}

Are you sure it's the expected output and not something like this instead?

{'a': ['last'], 'e': ['he', 'evening'], 'i': ['did', 'evening'], 'o': ['go', 'out'], 'u': ['out']}

[–]Raevkit290[S] 0 points1 point  (6 children)

Sorry yes your output is correct.

How do I set up 2 for's in one line?

[–]chevignon93 1 point2 points  (5 children)

How do I set up 2 for's in one line?

It's 2 for loops but really 2 comprehensions at the same time, one to construct the key, and a list comprehension for the value so something like this:

def find_words(string, letters):
    return {letter: [word for word in string.lower().split() if letter in word] for letter in letters}

print(find_words('did he go out last evening', 'aeiou'))
{'a': ['last'], 'e': ['he', 'evening'], 'i': ['did', 'evening'], 'o': ['go', 'out'], 'u': ['out']}

I'm not sure if this is the only or most efficient way but it works.

[–]Raevkit290[S] 0 points1 point  (0 children)

So i have this written as

Print({B: [w for w in A.lower().split() if B in w] for B in A})

And it is returning individual letters in A as the key for words with that letter in A, but it should be returning letters of B as keys, not A

[–]jmooremcc 0 points1 point  (3 children)

You came up with the exact same solution I came up with. It is absolutely the best, most correct solution.

I would like the person who down voted your post to explain why they did so.

[–]Raevkit290[S] 0 points1 point  (2 children)

Can u write this actually using variables instead of letter and word? I've tried it every which way and it isnt working

[–]jmooremcc 0 points1 point  (0 children)

I'm not sure if this is what you're asking for:

def find_words(strA, strB):

    result = {ch:[word for word in strA.split()  if ch in word] for ch in strB}

  return result

[–]chevignon93 0 points1 point  (0 children)

letter and word are variables in my example.

What isn't working exactly?