Python finding a value in a list of lists

前端 未结 2 1619
南笙
南笙 2021-01-17 02:19

Recently I had to find which list something was in. I used:

def findPoint(haystack, needle): # haystack = [[1,2,3], [4,5]...,[6,7,8,9]]
    for x in range(le         


        
2条回答
  •  醉酒成梦
    2021-01-17 02:55

    Yes, no need for range, for starters

    for hay in haystack:
      if needle in hay:
        return hay
    

    And if you really really need the index, use enumerate

    for x, hay in enumerate(haystack):
      if needle in hay:
        return x
    

提交回复
热议问题