i have list of list, each first items of the second level list can be seen as a kind of metainformation 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)]
natures are available here:
natures = list(set(zip(*L)))[0]
i need to build another list having each of each different possible combinations grouping them by consecutive of each "nature", (that is the natures
)
a result should be from the example below
R = [
[('n0', 1), ('n1', 4), ('n2', 5)],
[('n0', 1), ('n1', 2), ('n2', 5)]
]
i think that this can be done cleverly using some of the itertools package, but i'm totally lost inside of it, can someone help me on the right itertools stuff to use (groupby
and product
maybe ?)
best regards