我有一个元组(Name, val 1, val 2, Class)
tuple = (("Jackson",10,12,"A"),
("Ryan",10,20,"A"),
("Michael",10,12,"B"),
("Andrew",10,20,"B"),
("McKensie",10,12,"C"),
("Alex",10,20,"D"))
我需要使用不重复类的itertools combinations返回所有组合。如何返回不重复课程的组合。例如,第一个返回的语句是:tuple0, tuple2, tuple4, tuple5
,依此类推。
答案 0 :(得分:5)
组(按班级):
>>> ts = (("Jackson",10,12,"A"),
... ("Ryan",10,20,"A"),
... ("Michael",10,12,"B"),
... ("Andrew",10,20,"B"),
... ("McKensie",10,12,"C"),
... ("Alex",10,20,"D"))
>>> import itertools
>>> import operator
>>>
>>> by_class = operator.itemgetter(3)
>>>
>>> tuple_grps = [list(grp) for key, grp in itertools.groupby(sorted(ts, key=by_class), key=by_class)]
>>> tuple_grps
[[('Jackson', 10, 12, 'A'), ('Ryan', 10, 20, 'A')],
[('Michael', 10, 12, 'B'), ('Andrew', 10, 20, 'B')],
[('McKensie', 10, 12, 'C')],
[('Alex', 10, 20, 'D')]]
然后,使用itertools.product
获得所需的结果:
>>> for xs in itertools.product(*tuple_grps):
... print(xs)
...
(('Jackson', 10, 12, 'A'), ('Michael', 10, 12, 'B'), ('McKensie', 10, 12, 'C'), ('Alex', 10, 20, 'D'))
(('Jackson', 10, 12, 'A'), ('Andrew', 10, 20, 'B'), ('McKensie', 10, 12, 'C'), ('Alex', 10, 20, 'D'))
(('Ryan', 10, 20, 'A'), ('Michael', 10, 12, 'B'), ('McKensie', 10, 12, 'C'), ('Alex', 10, 20, 'D'))
(('Ryan', 10, 20, 'A'), ('Andrew', 10, 20, 'B'), ('McKensie', 10, 12, 'C'), ('Alex', 10, 20, 'D'))
获得任意长度的组合:
>>> for i in range(1, len(tuple_grps)+1):
... for xs in itertools.combinations(tuple_grps, i):
... for ys in itertools.product(*xs):
... print(ys)
...
(('Jackson', 10, 12, 'A'),)
(('Ryan', 10, 20, 'A'),)
(('Michael', 10, 12, 'B'),)
(('Andrew', 10, 20, 'B'),)
(('McKensie', 10, 12, 'C'),)
(('Alex', 10, 20, 'D'),)
(('Jackson', 10, 12, 'A'), ('Michael', 10, 12, 'B'))
(('Jackson', 10, 12, 'A'), ('Andrew', 10, 20, 'B'))
(('Ryan', 10, 20, 'A'), ('Michael', 10, 12, 'B'))
(('Ryan', 10, 20, 'A'), ('Andrew', 10, 20, 'B'))
...
(('Ryan', 10, 20, 'A'), ('Andrew', 10, 20, 'B'), ('McKensie', 10, 12, 'C'), ('Alex', 10, 20, 'D'))