You can just call itertools.combinations()
with r=2
to get all pairs of each triple and then flatten, e.g.:
In []:
import itertools as it
[t for x in it.product(*s) for t in it.combinations(x, r=2)]
Out[]:
[('a0', 'b0'), ('a0', 'c0'), ('b0', 'c0'), ('a0', 'b0'), ('a0', 'c1'), ('b0', 'c1'),
('a0', 'b0'), ('a0', 'c2'), ('b0', 'c2'), ('a0', 'b1'), ('a0', 'c0'), ('b1', 'c0'),
('a0', 'b1'), ('a0', 'c1'), ('b1', 'c1'), ('a0', 'b1'), ('a0', 'c2'), ('b1', 'c2'),
('a1', 'b0'), ('a1', 'c0'), ('b0', 'c0'), ('a1', 'b0'), ('a1', 'c1'), ('b0', 'c1'),
('a1', 'b0'), ('a1', 'c2'), ('b0', 'c2'), ('a1', 'b1'), ('a1', 'c0'), ('b1', 'c0'),
('a1', 'b1'), ('a1', 'c1'), ('b1', 'c1'), ('a1', 'b1'), ('a1', 'c2'), ('b1', 'c2')]
However, this will have duplicates because of ('a0', 'b0', 'c0')
, ('a0', 'b0', 'c1')
and ('a0', 'b0', 'c2')
all generate ('a0', 'b0')
, so if you want to remove then you can just use a set
, but you lose order:
In []:
{t for x in it.product(*s) for t in it.combinations(x, r=2)}
Out[]:
{('a1', 'c0'), ('b0', 'c1'), ('b0', 'c2'), ('a0', 'c1'), ('a0', 'c2'), ('b1', 'c1'),
('b1', 'c2'), ('a1', 'b1'), ('a1', 'c1'), ('a1', 'c2'), ('b0', 'c0'), ('a0', 'c0'),
('a1', 'b0'), ('b1', 'c0'), ('a0', 'b1'), ('a0', 'b0')}
Sorting it does put it back in order:
In []:
sorted({t for x in it.product(*s) for t in it.combinations(x, r=2)})
Out[]:
[('a0', 'b0'), ('a0', 'b1'), ('a0', 'c0'), ('a0', 'c1'), ('a0', 'c2'), ('a1', 'b0'),
('a1', 'b1'), ('a1', 'c0'), ('a1', 'c1'), ('a1', 'c2'), ('b0', 'c0'), ('b0', 'c1'),
('b0', 'c2'), ('b1', 'c0'), ('b1', 'c1'), ('b1', 'c2')]