Suppose we have a list, and a list of lists. I want to replace all values in each sub-list of the latter such that the value i is replaced by the value in the i-th position of the former. For example:
l = [4,2,1,0,3]
l_of_l = [[0,3],[4,1,2,3],[2,4]]
desired_output = [[4,0],[3,2,1,0],[1,3]]
All 0's are replaced by l[0], which equals 4. All 1's are replacd by l[1], which equals 2. And so on.
All the numbers appear just once in l, and all numbers between the maximum and the minimum are there. Namely, if the minimum value is 0 and the maximum is 6, then the list will be a shuffled version of list(range(7)). Also, all numbers present in the lists that l_of_l contains are present in l (i.e. if the maximum value in l is 6, there will not be any number greater than 6 in any list of l_of_l).
I've done this:
for this_l in l_of_l:
for i in range(len(this_l)):
this_l[i] = l[this_l[i]]
But I'm not very fond of loops. Is there a more pythonic way to do this?