嵌套列表的“in”运算符的替代方法

时间:2020-12-03 19:45:56

If I want to find something in a list in python I can use the 'in' operator:

如果我想在python的列表中找到一些东西,我可以使用'in'运算符:

list = ['foo', 'bar']
'foo' in list #returns True

But what should I do if I want to find something in a nested list?

但是,如果我想在嵌套列表中找到某些东西,我该怎么办?

list = [('foo', 'bar'), ('bar', 'foo')]
'foo' in list #returns False

Is it possible to do it in one row without a for loop for example?

例如,可以在没有for循环的情况下在一行中执行此操作吗?

Thanks!

5 个解决方案

#1


8  

You probably want any:

你可能想要任何:

>>> list = [('foo', 'bar'), ('bar', 'foo')]
>>> any('foo' in e for e in list)
True

Some sort of loop is inevitable though.

但是某种循环是不可避免的。

#2


2  

It's abusive, but you can do this in one line pretty easily.

这是滥用,但你可以很容易地在一行中做到这一点。

mainlist = [('foo', 'bar'), ('bar', 'foo')]
[elem for elem in sublist for sublist in mainlist] #['bar', 'bar', 'foo', 'foo']

'foo' in [elem for elem in sublist for sublist in mainlist] # True

#3


1  

You could use itertools.chain like that :

您可以像这样使用itertools.chain:

from itertools import chain

nested__seq = [(1,2,3), (4,5,6)]

print 4 in chain(*nested__seq)

PS : you shouldn't override bultins like "list"

PS:你不应该像“列表”那样覆盖bultins

#4


1  

If you have a list of iterables with arbitrary depth, flatten it first:

如果您有一个具有任意深度的迭代列表,请先将其展平:

import collections 

li= [('foo', 'bar'), ('bar', 'foo'),[[('deeper',('foobar'))]]]

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

print 'deeper' in flatten(li)  
print 'foo' in flatten(li) 
print 'nope' in flatten(li)

Prints:

True
True
False

#5


1  

You could also do this with in

你也可以用in来做到这一点

>>> list = [('foo', 'bar'), ('bar', 'foo')]
>>> 'foo' in (x[1] for x in list)
True

EDIT: this method check only if foo is as fist element.

编辑:此方法仅检查foo是否为第一元素。

To search as 'foo' a element (any):

要搜索'foo'元素(any):

 >>>'foo' in reduce(lambda x,y: x+y, list)   
 True

Some more try:

还有一些尝试:

In [7]: list
Out[7]: [('foo', 'me', 'bar'), ('bar', 'foo', 'you')]
In [8]: 'me' in reduce(lambda x,y: x+y, list)
Out[8]: True

In [9]: 'you' in reduce(lambda x,y: x+y, list)
Out[9]: True

#1


8  

You probably want any:

你可能想要任何:

>>> list = [('foo', 'bar'), ('bar', 'foo')]
>>> any('foo' in e for e in list)
True

Some sort of loop is inevitable though.

但是某种循环是不可避免的。

#2


2  

It's abusive, but you can do this in one line pretty easily.

这是滥用,但你可以很容易地在一行中做到这一点。

mainlist = [('foo', 'bar'), ('bar', 'foo')]
[elem for elem in sublist for sublist in mainlist] #['bar', 'bar', 'foo', 'foo']

'foo' in [elem for elem in sublist for sublist in mainlist] # True

#3


1  

You could use itertools.chain like that :

您可以像这样使用itertools.chain:

from itertools import chain

nested__seq = [(1,2,3), (4,5,6)]

print 4 in chain(*nested__seq)

PS : you shouldn't override bultins like "list"

PS:你不应该像“列表”那样覆盖bultins

#4


1  

If you have a list of iterables with arbitrary depth, flatten it first:

如果您有一个具有任意深度的迭代列表,请先将其展平:

import collections 

li= [('foo', 'bar'), ('bar', 'foo'),[[('deeper',('foobar'))]]]

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

print 'deeper' in flatten(li)  
print 'foo' in flatten(li) 
print 'nope' in flatten(li)

Prints:

True
True
False

#5


1  

You could also do this with in

你也可以用in来做到这一点

>>> list = [('foo', 'bar'), ('bar', 'foo')]
>>> 'foo' in (x[1] for x in list)
True

EDIT: this method check only if foo is as fist element.

编辑:此方法仅检查foo是否为第一元素。

To search as 'foo' a element (any):

要搜索'foo'元素(any):

 >>>'foo' in reduce(lambda x,y: x+y, list)   
 True

Some more try:

还有一些尝试:

In [7]: list
Out[7]: [('foo', 'me', 'bar'), ('bar', 'foo', 'you')]
In [8]: 'me' in reduce(lambda x,y: x+y, list)
Out[8]: True

In [9]: 'you' in reduce(lambda x,y: x+y, list)
Out[9]: True