这是一个用于对任意数量列表中的组合进行分组的通用函数:
>>> def grouped_combos(n, *ls):
... for comb in itertools.combinations(zip(*ls), n):
... yield comb
>>> list(grouped_combos(2, [256, 456, 24, 79, 14], ['a', 'aa', 'dd', 'e', 'b']))
[((256, 'a'), (456, 'aa')),
((256, 'a'), (24, 'dd')),
((256, 'a'), (79, 'e')),
((256, 'a'), (14, 'b')),
((456, 'aa'), (24, 'dd')),
((456, 'aa'), (79, 'e')),
((456, 'aa'), (14, 'b')),
((24, 'dd'), (79, 'e')),
((24, 'dd'), (14, 'b')),
((79, 'e'), (14, 'b'))]
>>> list(grouped_combos(4, [1, 2, 3, 4, 5], "abcde", "xyzzy"))
[((1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z'), (4, 'd', 'z')),
((1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z'), (5, 'e', 'y')),
((1, 'a', 'x'), (2, 'b', 'y'), (4, 'd', 'z'), (5, 'e', 'y')),
((1, 'a', 'x'), (3, 'c', 'z'), (4, 'd', 'z'), (5, 'e', 'y')),
((2, 'b', 'y'), (3, 'c', 'z'), (4, 'd', 'z'), (5, 'e', 'y'))]