Reputation: 111
I have a list of lists and i want to merge the lists with an specific order. See example:
id list 0 1 2
[[0], [2, 6, 1, 4], [3, 7, 5]]
Order Resulting List
[1, 0, 2] = [2, 6, 1, 4, 0, 3, 7, 5]
[0, 2, 1] = [0, 3, 7, 5, 2, 6, 1, 4]
[2, 1, 0] = [3, 7, 5, 2, 6, 1, 4, 0]
Someone can suggest a more elegant algorithm that proposed below?
groups = [[0], [2, 6, 1, 4], [3, 7, 5]]
orders = [[1, 0, 2], [0, 2, 1], [2, 1, 0]]
for order in orders:
LC = []
for i in order:
LC += groups[i]
return LC
Let me explain a bit better what I need:
groups = [[0], [2, 6, 1, 4], [3, 7, 5]]
orders = [[0, 2, 1], [1, 0, 2], [2, 1, 0]] # Order of each group in LC
solutions = [] # I want to put the created LC here
for order in orders:
LC = [] # I need this because a want LCs individualy and not one with all
for i in order: # for each order I pick de index (i) of the group
LC += grupos[i] # and merge then according with index of group
solutions.append([LC])
print(solutions)
I want this (one LC for each order):
[[0, 3, 7, 5, 2, 6, 1, 4], [2, 6, 1, 4, 0, 3, 7, 5], [3, 7, 5, 2, 6, 1, 4, 0]]
and not this:
[0, 3, 7, 5, 2, 6, 1, 4, 2, 6, 1, 4, 0, 3, 7, 5, 3, 7, 5, 2, 6, 1, 4, 0]
The algorithm above works, but a need a another one more elegant and efficient.
Some examples of output:
groups = [[0], [2, 1], [3, 7, 5], [4], [6]]
Order = [1, 0, 2, 3, 4]
LC = [2, 1, 0, 3, 7, 5, 4, 6]
[2, 1, 0, 3, 4]
[3, 7, 5, 2, 1, 0, 4, 6]
[3, 1, 2, 0, 4]
[4, 2, 1, 3, 7, 5, 0, 6]
[4, 1, 2, 3, 0]
[6, 2, 1, 3, 7, 5, 4, 0]
[0, 2, 1, 3, 4]
[0, 3, 7, 5, 2, 1, 4, 6]
[0, 3, 2, 1, 4]
[0, 4, 3, 7, 5, 2, 1, 6]
[0, 4, 2, 3, 1]
[0, 6, 3, 7, 5, 4, 2, 1]
[0, 1, 3, 2, 4]
[0, 2, 1, 4, 3, 7, 5, 6]
[0, 1, 4, 3, 2]
[0, 2, 1, 6, 4, 3, 7, 5]
[0, 1, 2, 4, 3]
[0, 2, 1, 3, 7, 5, 6, 4]
Upvotes: 0
Views: 727
Reputation: 180542
Just call itertools.chain on the indexes and combine with with operator.itemgetter:
frIn [9]: groups = [[0], [2, 6, 1, 4], [3, 7, 5]]
In [10]: orders = [[0, 2, 1], [1, 0, 2], [2, 1, 0]] # Ord
In [11]: from itertools import chain
In [12]: from operator import itemgetter
In [13]: [list(chain(*itemgetter(*o)(groups))) for o in orders]
[[0, 3, 7, 5, 2, 6, 1, 4], [2, 6, 1, 4, 0, 3, 7, 5], [3, 7, 5, 2, 6, 1, 4, 0]]
In you own code, you only return the last LC so it could not work correctly:
for order in orders:
LC = [] # overwritten each iteration so you only get the last sublists.
for i in order:
LC += parts[i]
return LC
Upvotes: 1
Reputation: 49330
You could use some other technique like a comprehension. The following will return a flat list:
return [part for order in orders for i in order for part in parts[i]]
And the following will return a 2D list:
return [[part for i in order for part in parts[i]] for order in orders]
Upvotes: 2
Reputation: 66
inputs = [[1,2,3], [4,5,6], [7]]
orders = [[0,1,2], [2,1,0]]
result = [input_element for order in orders for order_element in order for input_element in inputs[order_element]]
print(result)
Upvotes: 0
Reputation: 12515
This solution is basically identical to the one you proposed, but more Python-esque, using a list comprehension.
>>> def merge_lists(desired_order):
... merged_list = [element for i in desired_order for element in parts[i]]
... return merged_list
...
>>> desired_order = orders[0]
>>> merge_lists(desired_order)
[2, 6, 1, 4, 0, 3, 7, 5]
Upvotes: 1