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. 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)
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]
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).

-1
url = 'http://www.google.com'
blocks = ['youtube.com','google.com','bing.com']
return not(any([block in url for block in blocks]))
1
  • there is a result just False
    – xiº
    Oct 8, 2015 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.