searching within nested list in python

前端 未结 4 722
鱼传尺愫
鱼传尺愫 2021-01-13 18:56

I have a list:

l = [[\'en\', 60, \'command\'],[\'sq\', 34, \'komand\']]

I want to search for komand or sq and get

4条回答
  •  梦谈多话
    2021-01-13 19:01

    If all you're trying to do is return the first list that contains a match for any of the values then this will work.

    def search(inlist, matches):
        for li in inlist:
            for m in matches:
                if m in li:
                    return li
        return None
    
    >>> l = [['en', 60, 'command'],['sq', 34, 'komand']]
    >>> search(l, ('sq', 'komand'))
    ['sq', 34, 'komand']
    

提交回复
热议问题