python - 确定字符串列表中的条件

标签 python python-3.4

我习惯了 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/

相关文章:

python - 如何创建 Cocoa 库并在 python 中使用它

python - 从python中的文本文件中过滤一些行

python - python 字符串格式错误

python-3.4 - ldap3 操作 : SEARCH seems to fail (search_filter syntax vs RFC4515)

pyside - "Module use of python34.dll conflicts with this version of Python"

python - 获取最长字符串长度的函数

python - PANDAS:尝试应用多个函数时如何访问 groupby 对象的键

python - matplotlib.pyplot 在相等范围内绘制 x 轴刻度

python-2.7 - 在 Python 2.7 和 Python 3.4 中带有内存 View 的 Ctypes from_buffer

python - 无法取消 pickle 对象