确定字符串列表中的条件

时间:2015-07-05 15:41:56

标签: 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

4 个答案:

答案 0 :(得分:6)

听起来像你的意思:

int xPos = (Int32)dtData.Rows[3][0];//you know which access fourth row and first column?

或更简单:

def isGood(linesForItem):
    result = True
    if any('ERROR' in line for line in linesForItem):
        result = False
    return result

答案 1 :(得分:2)

有时无聊的解决方案也是最快的解决方案之一......

def isGood(linesForItem):
    for line in linesForItem:
        if "ERROR" in line:
            return False
    return True

一旦找到匹配项,也会停止。

答案 2 :(得分:1)

问题是您必须检查列表中每个元素的var trElem = $("#idOfTrElem").get();//This gives me an array of the content of TR element. 子字符串。目前,您只需检查该字符串是否在数组中。这一行将为您提供答案。

'ERROR'
  • any(map(lambda o: 'ERROR' in o, linesForItem)) - 如果True列表中至少有一个元素包含linesForItem substring
  • 'ERROR' - 否则

然后将它包装在你的函数中,如下所示:

False

答案 3 :(得分:0)

我假设错误&#39;错误&#39;嵌入在文本字符串中,并且列表中的所有项都是文本字符串。有几种方法,这是我的最爱:

CREATE PROCEDURE atsp_GetNameID 
    (@CustomerID AS INT)
AS
BEGIN
    SELECT ContactID
    FROM atbv_Contacts
    WHERE ContactID LIKE '%' + @CustomerID + '%'
END

myList = ['stuff', 'xxxxERRORxxxx', '42'] if any([s.find('ERROR') > -1 for s in myList]): result = True else: result = False 方法返回字符串开头的位置,如果不存在则返回-1。