jeudi 16 juin 2016

Python: Return all Indices of every occurrence of a Sub List within a Main List


I have a Main List and a Sub List:

main_list = [1,2,3,4,4,4,1,2,3,4,4,4]
sub_list = [4,4,4]

I want to locate the indices of every occurrence of the Sub List that are found in the Main List, in this example, I want the follow List of Indices returned:

indices = [3,9]

I would prefer not to use numpy, if at all possible.

A second example, for clarity:

main_list = [9,8,7,5,5,5,5,5,4,3,2,5,5,5,5,5,1,1,1,5,5,5,5,5]
sub_list = [5,5,5,5,5]

indices = [3,11,19]

Aucun commentaire:

Enregistrer un commentaire