0

Is there a more efficient way to do something than this... I am just creating a block list for a bunch of URLs

url = 'http://www.google.com'
blocks = ['youtube.com','google.com','bing.com']
for block in blocks:
    if block in url:
        return 0
return 1
1
  • Algorithmically, you could store your blocks as a radix tree. Practically, in Python, you may not get much better performance than speeding up the iteration via comprehensions and other optimization tricks. Commented Oct 7, 2015 at 23:08

4 Answers 4

2
url = 'http://www.google.com'
blocks = ['youtube.com','google.com','bing.com']
return filter(lambda b: b in url, blocks)
Sign up to request clarification or add additional context in comments.

Comments

0

List comprehension is more preferable according to PEP.

>>> print [block in url for block in blocks]
[False, True, False]

or if you prefer int:

>>> print [int(block in url) for block in blocks]
[0, 1, 0]

Comments

0

If you are going to be doing this operation over and over again, it may give you a modest performance gain to precompile a regex containing all of the blocks. For example:

import re
blocks = ["youtube.com", "google.com", "bing.com"]
precomp_regex = re.compile("|".join(map(re.escape, blocks)))

def string_contains_block(string, regex=precomp_regex):
    return regex.search(string)

If your set of blocks is very large, or subject to change often, it may be worth storing as a radix trie. (Think of the OR'd regex as a very naive implementation of a radix trie).

Comments

-1
url = 'http://www.google.com'
blocks = ['youtube.com','google.com','bing.com']
return not(any([block in url for block in blocks]))

1 Comment

there is a result just False

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.