简体   繁体   English

如何检查字符串列表中是否存在字符串,包括子字符串?

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

I have written a function to check for the existence of a value in a list and return True if it exists. 我编写了一个函数来检查列表中是否存在值,如果存在则返回True。 It works well for exact matches, but I need for it to return True if the value exists anywhere in the list entry (eg value <= listEntry, I think.) Here is the code I am using for the function: 它适用于完全匹配,但如果值存在于列表条目中的任何位置,我需要它返回True(例如,值<= listEntry,我认为。)这是我用于函数的代码:

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? 如果不循环遍历整个字符串和/或使用RegEx(除非那些是最有效的),我应该如何以pythonic方式进行此操作?

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. 这与另一个SO问题非常相似,但我需要检查列表中任何位置是否存在ENTIRE val字符串。 It would also be great to return the index / indices of matches, but I'm sure that's covered elsewhere on Stackoverflow. 返回匹配的索引/索引也很棒,但我确信Stackoverflow上的其他内容已经覆盖了。

If I understood your question then I guess you need any : 如果我理解你的问题,那么我猜你需要any

return any(val in x for x in lst)

Demo: 演示:

>>> lst = ['aaa','dfbbsd','sdfdee']
>>> val = 'bb'
>>> any(val in x  for x in lst)
True
>>> val = "foo"
>>> any(val in x  for x in lst)
False
>>> val = "fde"
>>> any(val in x  for x in lst)
True

There are multiple possibilities to do that. 这样做有多种可能性。 For example: 例如:

def valInList1 (val, lst):
    # check `in` for each element in the list
    return any(val in x for x in lst)

def valInList2 (val, lst):
    # join the list to a single string using some character
    # that definitely does not occur in val
    return val in ';;;'.join(lst)

Mostly covered, but if you want to get the index of the matches I would suggest something like this: 大部分都被覆盖了,但是如果你想获得匹配的索引,我会建议这样的事情:

indices = [index for index, content in enumerate(input) if substring in content]

if you want to add in the true/false you can still directly use the result from this list comprehension since it will return an empty list if your input doesn't contain the substring which will evaluate to False . 如果要添加true/false您仍然可以直接使用此列表推导的结果,因为如果您的输入不包含将评估为False的子字符串,它将返回一个空列表。

In the terms of your first function: 在你的第一个功能方面:

def isValInLst(val, lst):
    return bool([index for index, content in enumerate(lst) if val in content])

where the bool() just converts the answer into a boolean value, but without the bool this will return a list of all places where the substring appears in the list. bool()只是将答案转换为布尔值,但没有bool,这将返回列表中出现子字符串的所有位置的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM