简体   繁体   中英

Python: finding the indices of a list that match the given sublist

Suppose there are two Python Lists:

my_list = ['a', 'bb', 'c', 'bb', 'cc', 'bbc']
sublist = ['bb', 'bb', 'bbc']

Apparently, sublist consists of the 1st, 3rd, and 5th elements of *my_list*. Now I want to get the indices [1, 3, 5] , given these two lists. Is there any simple way to do this?

Note that:

  1. The list CAN have same elements.
  2. The sublist maintains the order of the elements in the original list.

You can use a generator:

def indices(lst, items):
    last_index = 0

    for item in items:
        last_index += lst.index(item, last_index + 1) + 1

        yield last_index

@Blender's approach is great for lists (which is all this problem entails), this method is more generalized for all iterables

>>> def indices(a, b): # find indices of items from b inside a
        enumerate_a = enumerate(a)
        for x in b:
            for i, y in enumerate_a:
                if x == y:
                    yield i
                    break


>>> list(indices(['a', 'bb', 'c', 'bb', 'cc', 'bbc'], ['bb', 'bb', 'bbc']))
[1, 3, 5]
def get_indices(lst, sublist):
    result = []
    i = 0
    for x in sublist:
        result.append(lst.index(x, i))
        i = result[-1] + 1
    return result

Examples:

>>> get_indices(['a', 'bb', 'c', 'bb', 'cc', 'bbc'], ['bb', 'bb', 'bbc'])
[1, 3, 5]
>>> get_indices(['a', 'bb', 'c', 'bb', 'cc', 'bbc'], ['c', 'bb', 'bbc'])
[2, 3, 5]

My solution

def getIndex(list, sublist):
    index = []
    j = 0;
    for i in range(len(list)):
        if(sublist[j] == list[i]):
            index.append(i)
            j += 1

    return index

It works like ?

$ python -i p.py 
>>> getIndex(['bb', 'aa', 'bb'], ['aa', 'bb'])
[1, 2]
>>> getIndex(['a', 'bb', 'c', 'bb', 'cc', 'bbc'], ['bb', 'bb', 'bbc'])
[1, 3, 5]
>>> getIndex(['a', 'bb', 'c','bbc', 'bb', 'cc', 'bbc'], ['bb', 'bb', 'bbc'])
[1, 4, 6]

Please some one let me know if I am still wrong.

I like Blender 's answer most. but i don't think index() function has a parameter 'start' so I modified a little at the below.

idx = 0
start = 0
for i in sublist:
    idx = my_list[start:].index(i) + start
    start = idx + 1
    yield idx

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