I have a list of lists, every first element of a list of the second level can be considered as a kind of meta information about its nature.
# simple sample, real data are much more complex, but it can be schematized as this one L = [('n0', 1), ('n1', 4), ('n1', 2), ('n2', 5)]
Nature is available here:
natures = list(set(zip(*L)))[0]
I need to build another list in which each of the various possible combinations is grouped by the sequences of each "nature" (that is, natures
)
the result should be shown in the example below
R = [ [('n0', 1), ('n1', 4), ('n2', 5)], [('n0', 1), ('n1', 2), ('n2', 5)] ]
I think this can be done with the clever use of some itertools package, but I am completely lost inside it, can someone help me on the proper use of itertools ( groupby
and product
possible?)
Regards