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


this question has answer here:

i have main list , sub list , want locate indices of every occurrence of sub list found in main list, in example, want following list of indices returned.

>>> main_list = [1,2,3,4,4,4,1,2,3,4,4,4] >>> sub_list = [4,4,4]  >>> function(main_list, sub_list) >>> [3,9] 

ideally, function should ignore fragments of sub_list, in case [4,4] ignored. also, expect elements single digit integers. here second example, 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]  >>> function(main_list, sub_list) >>> [3,11,19] 

maybe using strings way go?

import re original = ''.join([str(x) x in main_list]) matching = ''.join([str(x) x in sub_list]) starts = [match.start() match in re.finditer(re.escape(matching), original)] 

the problem 1 doesn't count overlapping values


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -