Suppose I have a python list as: points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]
And I need to split this list containing the number of elements in the index_list=[2, 2, 6, 3] But having common endpoints
That is:
- First 2 elements from
points_list:[1,2] - Next 2 elements from the
points_listbut it should start from the place it stopped before :[2,3] - Then the next 6 elements :
[3,4,5,6,7,8] - Finally, 3 elements in the same way :
[8,9,1]
Ultimately, what I expect is to have something like: [[1,2],[2,3],[3,4,5,6,7,8],[8,9,1]] which corresponds to the number of elements mentioned in the index_list=[2, 2, 6, 3]
Can you please help me to perform this task
>Solution :
Here’s my code:
points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]
index_list=[2, 2, 6, 3]
res = []
end_point = 0
for i in index_list:
temp_list = points_list[end_point:end_point+i] # Get the list
res.append(temp_list) # Append it
end_point = points_list.index(temp_list[-1]) # Get the index of the last value
print(res)