检查列表元素是否都不包含搜索的子字符串

时间:2013-06-06 16:14:08

标签: python list substring

我有清单:

myList = ['qwer', 'tyu', 'iop12', '3456789']

如何检查列表中的所有元素是否都包含搜索的子字符串

  • for string 'wer' result应为False(包含子字符串的exists元素)
  • for string '123' result应该为True(没有元素包含这样的子字符串)

3 个答案:

答案 0 :(得分:2)

not any(search in s for s in myList)

或者:

all(search not in s for s in myList)

例如:

>>> myList = ['qwer', 'tyu', 'iop12', '3456789']
>>> not any('wer' in s for s in myList)
False
>>> not any('123' in s for s in myList)
True

答案 1 :(得分:1)

您可以使用any

>>> myList = ['qwer', 'tyu', 'iop12', '3456789']
>>> not any('wer'  in x for x in myList)
False
>>> not any('123' in x for x in myList)
True

答案 2 :(得分:1)

内置anyall功能非常有用。

not any(substring in element for element in myList)

测试运行显示

>>> myList = ['qwer', 'tyu', 'iop12', '3456789']
>>> substring = 'wer'
>>> not any(substring in element for element in myList)
False
>>> substring = '123'
>>> not any(substring in element for element in myList)
True