Given list_a and list_b. I want to run list_b through a function that gives all possible sublist of list_b (this part of the code works). I then want to take every sublist of list_b, and see if that sublist is also a sublist of list_a. If it is I should get a list of all the indexes, or splices where that sublist appears in list_a.
I'm able to get the code to work for sublist of length one, but cannot get it to work for longer lists.
Here's my current code to solve this problem:
import numpy as np
a = [0,1,2,3,0,2,3]
b = [0,2,3]
sublists = []
def all_sublists(my_list):
""" make a list containg every sublist of a my_list"""
for i in range(len(my_list)):
n = i+1
while n <= len(my_list):
sublist = my_list[i:n]
sublists.append(sublist)
n += 1
def sublists_splice(sublist, my_list):
"""if sublist is in my_list print sublist and the corresponding indexes"""
values = np.array(my_list)
print(str(sublist) + " found at " + str(np.where(values == sublist)[0]))
all_sublists(b)
for sublist in sublists:
sublists_splice(sublist, a)
This is the output of the code:
[0] found at [0 4]
[0, 2] found at []
[0, 2, 3] found at []
[2] found at [2 5]
[2, 3] found at []
[3] found at [3 6]
/home/nicholas/Desktop/sublists.py:27: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
Here's what I'd like to get:
[0] found at [0 4]
[0, 2] found at [4:6]
[0, 2, 3] found at [4:7]
[2] found at [2 5]
[2, 3] found at [2:4 5:7]
[3] found at [3 6]
I'm assuming there's a pythonic way to approach this. While I've tried a few bits of code they've all been very long and haven't worked...
One last note. I do need them to be sublists not subsets as order matters.
I appreciate any help. Thank you.