tlre0952b
tlre0952b

Reputation: 751

Search a string for values present in a dict

As the title suggests, I am trying to find values in a dict within a string. This relates to my post here: Python dictionary - value

My code is something like follows:

import mechanize
from bs4 import BeautifulSoup

leaveOut = {
            'a':'cat',
            'b':'dog',
            'c':'werewolf',
            'd':'vampire',
            'e':'nightmare'
            }

br = mechanize.Browser()
r = br.open("http://<a_website_containing_a_list_of_movie_titles/")
html = r.read()
soup = BeautifulSoup(html)
table = soup.find_all('table')[0]

for row in table.find_all('tr'):
    # Find all table data
    for data in row.find_all('td'):
        code_handling_the_assignment_of_movie_title_to_var_movieTitle

        if any(movieTitle.find(leaveOut[c]) < 1 for c in 'abcde'):
            do_this_set_of_instructions
        else:
             pass

I want to skip the program contained under the if block (identified above as do_this_set_of_instructions) if the string stored in movieTitle contains any of the strings (or values if you like) in the leaveOut dict.

So far, I have had no luck with any(movieTitle.find(leaveOut[c]) < 1 for c in 'abcde'): as it always returns True and the do_this_set_of_instructions always execute regardless.

Any ideas?

Upvotes: 0

Views: 81

Answers (1)

Blender
Blender

Reputation: 298176

.find() returns -1 if the substring isn't in the string that you're working on, so your any() call will return True if any of the words aren't in the title.

You may want to do something like this instead:

 if any(leaveOut[c] in movieTitle for c in 'abcde'):
     # One of the words was in the title

Or the opposite:

 if all(leaveOut[c] not in movieTitle for c in 'abcde'):
     # None of the words were in the title

Also, why are you using a dictionary like this? Why don't you just store the words in a list?

leave_out = ['dog', 'cat', 'wolf']

...

if all(word not in movieTitle for word in leave_out):
     # None of the words were in the title

Upvotes: 1

Related Questions