我需要根据是否存在字符串列表来创建一个接受/拒绝某些文本的脚本.
我有一个应该用作拒绝机制的关键字列表:
k_out = ['word1', 'word2', 'some larger text']
如果在下面列出的列表中找到任何这些字符串元素,则该列表应标记为已拒绝.这是应该检查的列表:
c_lst = ['This is some text that contains no rejected word', 'This is some larger text which means this list should be rejected']
这就是我所拥有的:
flag_r = False
for text in k_out:
for lst in c_lst:
if text in lst:
flag_r = True
有更多的pythonic方式来解决这个问题吗?
解决方法:
>>> k_out = ['word1', 'word2', 'some larger text']
>>> c_lst = ['This is some text that contains no rejected word', 'This is some larger text which means this list should be rejected']
>>> any(keyword in string for string in c_lst for keyword in k_out)
True
>>>
标签:python,list