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

后端 未结 3 2823
萌比男神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
    

提交回复
热议问题