How to determined if a 2 dimensional list contain a value?

后端 未结 4 1042
忘掉有多难
忘掉有多难 2020-12-30 06:30

I have a list like following

mylist = [(\'value1\', \'value2\', \'value3\'), (\'secval1\', \'secval2\', \'secval3\')]

how do I see if the l

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 07:06

    similar to any(), a solution that also supports short-circuiting :

    >>> from itertools import chain
    >>> mylist = [('value1', 'value2', 'value3'), ('secval1', 'secval2', 'secval3')]
    >>> 'value2' in chain(*mylist)
    True
    

    proof that it short-circuits like any():

    >>> it=chain(*mylist)
    >>> 'value2' in it
    True
    >>> list(it) #part of iterable still not traversed
    ['value3', 'secval1', 'secval2', 'secval3']
    

提交回复
热议问题