简体   繁体   中英

What's the fastest way to locate a list element within a list in python?

The list is similar to this:

[["12", "stuA", "stuB"], ["51", "stuC", "stuD"], ..., ["3234", "moreStuff", "andMore"]]

Now I need to locate an item (get index) only by its first value (eg "332" ). Is there any better way to do this, apart from iterating from the first one to compare with each value?

Code:

index = 0
for item in thelist:
    if item[0] == "332":
         print index

    index = index + 1

No. Without iterating you cannot find it, unless the list is already sorted. You can improve your code like this, with enumerate and list comprehension.

[index for index, item in enumerate(thelist) if item[0] == "332"]

This will give the indices of all the elements where the first element is 332 .

If you know that 332 occurs only once, you can do this

def getIndex():
    for index, item in enumerate(thelist):
       if item[0] == "332":
          return index

No-one's mentioned this yet, so I will - if you need to find an item by its value quickly (and presumably more than just once), you should change the data structure you use to be one that supports the kind of access you need. Lists support fast access by index, but not by item value. If you stored the information in a dict keyed by the first element in the lists, you would be able to find rows very quickly by that first value:

# Make a dict from the list of lists:
itemLookup = {item[0]: item for item in theList}

itemLookup["51"] # -> ["51", "stuC", "stuD"]

So the brief answer is no (although there is a quick-ish way using bisection if the list is sorted), the longer answer is use a dictionary if you want fast lookup.

If you can definitely guarantee the key you want exists exactly once, this could also work.

import itertools
itertools.ifilter(lambda x: x[1][0] == "332", enumerate(theList)).next()[0]

and would work for getting more than one occurrence if you modify it to use the generator object instead of immediately calling next as I do here.

If it's feasible, though, I would suggest moving the data to a dict format ( OrderedDict if position of occurrence matters) with these integers as the keys (since you can guarantee they are unique), or potentially to a pandas DataFrame with the integers as the index.

如果密钥恰好出现一次,

zip(*thelist)[0].index("332")

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