简体   繁体   中英

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

I have a list of lists eg

[["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 eg [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"). Ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM