在python中,如何知道字符串是否属于字符串列表的组合?

在python中,如何知道字符串是否属于字符串列表的组合?,python,list,Python,List,我有一个复杂的列表列表…具有唯一字符串,如下所示: L = [['A'],[['B','C'],['D'],[['E'],['F','G']]]] 我想知道某个字符串是否属于L 让我们举一个简单的例子: L = [['A'],[['B'],['C']]] 我想知道“B”是否在L中,所以不是列表['B'],而是字符串“B”。我怎么办 感谢使用递归 def is_present(string_to_check, nested_list): output = False for e

我有一个复杂的列表列表…具有唯一字符串,如下所示:

L = [['A'],[['B','C'],['D'],[['E'],['F','G']]]]
我想知道某个字符串是否属于L

让我们举一个简单的例子:

L = [['A'],[['B'],['C']]]
我想知道“B”是否在L中,所以不是列表['B'],而是字符串“B”。我怎么办

感谢使用递归

def is_present(string_to_check, nested_list):
    output = False
    for element in nested_list:
         if isinstance(element, list):
             output = output or is_present(string_to_check, element)
         else:
             output = output or string_to_check in element
    return output
使用递归

def is_present(string_to_check, nested_list):
    output = False
    for element in nested_list:
         if isinstance(element, list):
             output = output or is_present(string_to_check, element)
         else:
             output = output or string_to_check in element
    return output

在您的情况下,如果将原始列表展平,搜索将更容易

import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

flatten_L = list(flatten(L))

if 'B' in flatten_L:
    print(True)
else:
    print(False)

在您的情况下,如果将原始列表展平,搜索将更容易

import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

flatten_L = list(flatten(L))

if 'B' in flatten_L:
    print(True)
else:
    print(False)

虽然递归解决方案生成正确的结果,但在找到匹配项后,它会继续不必要地循环其余的列表元素。这是一个优化的版本,我相信它也更清晰:

def is_present(string_to_check, nested_list):
    for element in nested_list:
        if isinstance(element, list):
            if is_present(string_to_check, element):
                return True
        elif string_to_check in element:
            return True
    return False


L = [['A'],[['B','C'],['D'],[['E'],['F','G']]]]
print(is_present('B', L))
印刷品:

True

虽然递归解决方案生成正确的结果,但在找到匹配项后,它会继续不必要地循环其余的列表元素。这是一个优化的版本,我相信它也更清晰:

def is_present(string_to_check, nested_list):
    for element in nested_list:
        if isinstance(element, list):
            if is_present(string_to_check, element):
                return True
        elif string_to_check in element:
            return True
    return False


L = [['A'],[['B','C'],['D'],[['E'],['F','G']]]]
print(is_present('B', L))
印刷品:

True

较短的递归解决方案:

def exists(d, t):
  return any(i == t if not isinstance(i,list) else exists(i, t) for i in d)


L = [['A'],[['B'],['C']]]
print(exists(L, 'B'))
输出:

True

较短的递归解决方案:

def exists(d, t):
  return any(i == t if not isinstance(i,list) else exists(i, t) for i in d)


L = [['A'],[['B'],['C']]]
print(exists(L, 'B'))
输出:

True

到目前为止,您尝试了什么?那么,对于上面的示例,您的输出是什么?它是布尔值还是别的什么?谢谢你的提问,它在这里确实是布尔值,我想得到真实的结果。我在L的子列表中尝试了类似于子列表中任何“B”的东西,但没有成功:/AkshayNevrekar:你到目前为止尝试了什么?那么,你在上面的例子中的输出是什么?这是布尔值还是别的什么?谢谢你的提问,这确实是一个布尔值,我想得到真实的答案。我尝试过在L的子列表的子列表中使用任何“B”之类的东西,但没有成功:/AkshayNevrekar:非常感谢这个替代解决方案!最后,您的解决方案对我来说是最好的,因为它没有将字符串考虑到更大的字符串中。例如,如果我想检查'C',您的解决方案的L=['A',['BC','B']],它将发送False,而这正是我想要的。谢谢!非常感谢您提供此替代解决方案!最后,您的解决方案对我来说是最好的,因为它没有将字符串考虑到更大的字符串中。例如,如果我想检查'C',您的解决方案的L=['A',['BC','B']],它将发送False,而这正是我想要的。谢谢!非常感谢这个解决方案!非常感谢这个解决方案!