I have a list of lists of numbers. The numbers are in sequential order, ranging from 0 to n and each number only appears once. I want to loop over all the numbers in the list of lists and return the index of the list containing that number.
list_of_lists = [[2,3,4,5,6],[0,1,7]] desired_output = [1,1,0,0,0,0,0,1]
I came up with something, but I'm thinking there has to be a way to do this without doing a list comprehension for each number. Performance is an issue
def get_indices(list_of_lists): indices = *len([n for l in list_of_lists for n in l]) for i in range(len(indices)): indices[i] = [ k for k,n in enumerate(list_of_lists) if i in n] return indices