Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If string does not contain any of list of strings in python

Tags:

python

I have a list of strings, from which I want to locate every line that has 'http://' in it, but does not have 'lulz', 'lmfao', '.png', or any other items in a list of strings in it. How would I go about this?

My instincts tell me to use regular expressions, but I have a moral objection to witchcraft.

like image 291
directedition Avatar asked Mar 08 '12 00:03

directedition


People also ask

How do you check if a string contains in a list Python?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.

How do you check if list does not contains an item Python?

count() to check if the list contains. Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0. If it returns a positive integer greater than 0, it means the list contains the element.

How do you check if a string does not contain a character in Python?

How do you check if a string does not contain a character in Python? Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

How do you check if a list of string contains a string?

To check if string contains substring from a list of strings, iterate over list of strings, and for each item in the list, check if the item is present in the given string. a source string, in which we have to check if some substring is present.


3 Answers

Here is an option that is fairly extensible if the list of strings to exclude is large:

exclude = ['lulz', 'lmfao', '.png']
filter_func = lambda s: 'http://' in s and not any(x in s for x in exclude)

matching_lines = filter(filter_func, string_list)

List comprehension alternative:

matching_lines = [line for line in string_list if filter_func(line)]
like image 196
Andrew Clark Avatar answered Oct 23 '22 23:10

Andrew Clark


This is almost equivalent to F.J's solution, but uses generator expressions instead of lambda expressions and the filter function:

haystack = ['http://blah', 'http://lulz', 'blah blah', 'http://lmfao']
exclude = ['lulz', 'lmfao', '.png']

http_strings = (s for s in haystack if s.startswith('http://'))
result_strings = (s for s in http_strings if not any(e in s for e in exclude))

print list(result_strings)

When I run this it prints:

['http://blah']
like image 28
srgerg Avatar answered Oct 23 '22 21:10

srgerg


Try this:

for s in strings:
    if 'http://' in s and not 'lulz' in s and not 'lmfao' in s and not '.png' in s:
        # found it
        pass

Other option, if you need your options more flexible:

words = ('lmfao', '.png', 'lulz')
for s in strings:
    if 'http://' in s and all(map(lambda x, y: x not in y, words, list(s * len(words))):
        # found it
        pass
like image 2
Pablo Santa Cruz Avatar answered Oct 23 '22 23:10

Pablo Santa Cruz