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.
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.
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? 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 .
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.
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)]
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']
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With