检查项目是否在嵌套列表中

时间:2016-11-09 19:01:10

标签: python list

在检查后的简单列表中是微不足道的:

x = [1, 2, 3]

2 in x  -> True

但如果是列表,例如:

x = [[1, 2, 3], [2, 3, 4]]

2 in x   -> False

如何解决此问题才能返回True

8 个答案:

答案 0 :(得分:11)

使用内置的any功能尝试此操作。这是最惯用的解决方案,它也很有效,因为T[] create(@RequestBody T[] objects) { for( T object : objects ) { service.create(object); } } 短路并在找到第一场比赛后立即停止:

any

答案 1 :(得分:8)

这是一个适用于任何嵌套级别的递归版本。

def in_nested_list(my_list, item):
    """
    Determines if an item is in my_list, even if nested in a lower-level list.
    """
    if item in my_list:
        return True
    else:
        return any(in_nested_list(sublist, item) for sublist in my_list if isinstance(sublist, list))

以下是一些测试:

x = [1, 3, [1, 2, 3], [2, 3, 4], [3, 4, [], [2, 3, 'a']]]
print in_nested_list(x, 2)
print in_nested_list(x, 5)
print in_nested_list(x, 'a')
print in_nested_list(x, 'b')
print in_nested_list(x, [])
print in_nested_list(x, [1, 2])
print in_nested_list(x, [1, 2, 3])

True
False
True
False
True
False
True

答案 2 :(得分:4)

您可以使用set.issubset()itertools.chain()

In [55]: x = [[1, 2, 3], [2, 3, 4]]

In [56]: {4}.issubset(chain.from_iterable(x))
Out[56]: True

In [57]: {10}.issubset(chain.from_iterable(x))
Out[57]: False

您还可以有效地削减多个项目的成员资格:

In [70]: {2, 4}.issubset(chain.from_iterable(x))
Out[70]: True

In [71]: {2, 4, 10}.issubset(chain.from_iterable(x))
Out[71]: False

答案 3 :(得分:2)

这样可行:

for arr in x:
    if 2 in arr:
        print True
        break

我会推荐奥斯卡的答案,因为any是正确的选择。

答案 4 :(得分:1)

<强> TL; DR

x = [0, [1, 2, 3], [2, 3, [4, 5, [6], []], [7, 8]]]
def find_n(input_list, n):
    for el in input_list:
        if el == n or (isinstance(el, list) and find_n(el, n)):
            return True
    return False
print(find_n(x, 6))

请注意,有点有趣:

def find_n(input_list, n):
    return any([el == n or (isinstance(el, list) and find_n(el, n)) for el in input_list])
return (find_n(x, 6))

执行速度超过50%。

原始答案

如果深度大于2怎么办?这是通用案例的一种方法:

x = [0, [1, 2, 3], [2, 3, [4, 5, [6], []], [7, 8]]]

def flatten(input_list):
    flat_list = []
    for sublist_or_el in input_list:
        if isinstance(sublist_or_el, list):
            for sublist_or_el2 in flatten(sublist_or_el):
                flat_list.append(sublist_or_el2)
        else:
            flat_list.append(sublist_or_el)
    return flat_list

print(6 in flatten(x))

虽然不确定速度,但正如我所说,这是一种可能对某人有用的方法!

编辑 - 更好(更快)答案:

这减少了所花费的时间(如果发现n,实际上即使没有找到,实际上大约一半的时间......)通过提前返回。这比@Curt F。​​的答案略快,并且比创建假设最大深度为2(接受的答案)的函数要慢。

x = [0, [1, 2, 3], [2, 3, [4, 5, [6], []], [7, 8]]]
def find_n(input_list, n):
    flat_list = []
    for sublist_or_el in input_list:
        if isinstance(sublist_or_el, list):
            if find_n(sublist_or_el, n) == True:
                return True
        elif sublist_or_el == n:
            return True
    return False
print(find_n(x, 6))

快速计时(非常hacky,抱歉,今天忙!):

import time

x = [0, [1, 2, 3], [2, 3, [4, 5, [6], []], [7, 8]]]

def a():
    def flatten(input_list):
        flat_list = []
        for sublist_or_el in input_list:
            if isinstance(sublist_or_el, list):
                for sublist_or_el2 in flatten(sublist_or_el):
                    flat_list.append(sublist_or_el2)
            else:
                flat_list.append(sublist_or_el)
        return flat_list
    return (6 in flatten(x))

def b():
    def find_n(input_list, n):
        flat_list = []
        for sublist_or_el in input_list:
            if isinstance(sublist_or_el, list):
                if find_n(sublist_or_el, n) == True:
                    return True
            elif sublist_or_el == n:
                return True
        return False
    return (find_n(x, 6))


zz = 0
for i in range(100000):
    start_time = time.clock()
    res = a()
    zz += time.clock() - start_time
print(a())
print((zz)/100, "seconds")

zz = 0
for i in range(100000):
    start_time = time.clock()
    res = b()
    zz += time.clock() - start_time
print(b())
print((zz)/100, "seconds")

答案 5 :(得分:0)

尝试

 2 in [i for sublist in x  for i in sublist]

答案 6 :(得分:0)

我的代码基于ÓscarLópez的解决方案。他的解决方案并不是我的问题所需要的,但它给了我足够的信息来解决我的问题。因此,如果您在一个列表中嵌套了元素,并且需要查看它们是否在另一个嵌套列表中,那么这将起作用。

#!python2

lst1 = [['a', '1'], ['b', '2'], ['c', '3'], ['d', '4'], ['e', '5']]
lst2 = [['b', '2'], ['d', '4'], ['f', '6'], ['h', '8'], ['j', '10'], ['l', '12'], ['n', '14']]

# comparing by index 0, prints lst1 items that aren't in lst2
for i in lst1:
    if not any(i[0] in sublst for sublst in lst2):
        print i
'''
['a', '1']
['c', '3']
['e', '5']
'''

print

# comparing by index 0, prints lst2 items that aren't in lst1
for i in lst2:
    if not any(i[0] in sublst for sublst in lst1):
        print i
'''
['f', '6']
['h', '8']
['j', '10']
['l', '12']
['n', '14']
'''

答案 7 :(得分:0)

Óscar Lopez的答案非常好!我建议使用它。

但是,您可以使用 itertools 来展平列表并进行评估,因此:

import itertools

x = [[1, 2, 3], [2, 3, 4]]
2 in itertools.chain.from_iterable(x)

Output: True

此外,您可以通过理解使其“手动”:

x = [[1, 2, 3], [2, 3, 4]]
2 in [item for sub_list in x for item in sub_list]

Output: True

这些只是其他方法,祝您好运。