我习惯了 C# 和编写 python 脚本。我想确定列表中的任何字符串是否包含字符串“ERROR”。
在 C# 中我会做这样的事情:
string myMatch = "ERROR";
List<string> myList = new List<string>();
bool matches = myList.Any(x => x.Contains(myMatch));
我在 python 中的尝试告诉返回 TRUE
,即使该列表包含包含单词 ERROR
的字符串。
def isGood (linesForItem):
result = True;
if 'ERROR' in linesForItem:
result = False
return result
最佳答案
听起来像你的意思:
def isGood(linesForItem):
result = True
if any('ERROR' in line for line in linesForItem):
result = False
return result
或者更简单地说:
def isGood(linesForItem):
return not any('ERROR' in line for line in linesForItem)
关于python - 确定字符串列表中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232052/