[Python] How do I check existence of a string in a list of strings, including substrings?

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have written a function to check for the existence of a value in a list and return True if it exists. It works well for exact matches, but I need for it to return True if the value exists anywhere in the list entry (e.g. value <= listEntry, I think.) Here is the code I am using for the function:

    def isValInLst(val,lst):
    """check to see if val is in lst. If it doesn't NOT exist (i.e. != 0),
    return True. Otherwise return false."""
    if lst.count(val) != 0:
    return True
    else:
    print 'val is '+str(val)
    return False


    Without looping through the entire character string and/or using RegEx's (unless those are the most efficient), how should I go about this in a pythonic manner?

    This is very similar to another SO question, but I need to check for the existence of the ENTIRE val string anywhere in the list. It would also be great to return the index / indices of matches, but I'm sure that's covered elsewhere on Stackoverflow.

    Continue reading...

Compartilhe esta Página