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

后端 未结 3 2818
萌比男神i
萌比男神i 2021-02-20 18:35

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 str

相关标签:
3条回答
  • 2021-02-20 19:17

    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
    
    0 讨论(0)
  • 2021-02-20 19:37

    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)]
    
    0 讨论(0)
  • 2021-02-20 19:37

    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']
    
    0 讨论(0)
提交回复
热议问题