Finding where a pair of numbers are located in a list of lists in Python?

时间:2015-09-29 00:54:52

标签: python list

I have a list of lists e.g

[["text",[6,24,4,40],[12,6,11,10],[...]]]  

I would like to be able to find out the position of a pair of numbers e.g [4,40] but I only need the position of where it is in the overall list so in this case it would be 0 (excluding the position of "text"). I.e. For this example I want to know the position of list [6,24,4,40]?
So far I can only manage to do it with 1 number, not a pair of numbers.

1 个答案:

答案 0 :(得分:1)

def subfind(needle, haystack):
    """Returns index if found, None otherwise."""
    length = len(needle)
    index = 0
    for item in haystack:
        if isinstance(item, list):
            for ee in xrange(0, len(item) - length + 1):
                if item[ee:ee+length] == needle:
                   return index
            index += 1