I want to select multiple groups of three or more elements from a list, according to some indices.
I thought of using itemgetter
, but it does not work for multiple sets, for example
labels=['C1','C2','C3','C4','C5','C6','C7','C8','C10','C13','C14','C15']
indexlist = list(itertools.combinations(range(1, 10), 3))
ixs= [4,5]
a=[indexlist[ix] for ix in ixs]
from operator import itemgetter
print(*itemgetter(*a[0])(labels))
where
a=[(1, 2, 7), (1, 2, 8)]
works well, whereas
labels=['C1','C2','C3','C4','C5','C6','C7','C8','C10','C13','C14','C15']
indexlist = list(itertools.combinations(range(1, 10), 3))
ixs= [4,5]
a=[indexlist[ix] for ix in ixs]
from operator import itemgetter
print(*itemgetter(*a)(labels))
gives the error
list indices must be integers or slices, not list
Is there a way to pass multiple sets of indices to itemgetter
, or is there some other convenient alternative?