我希望有一个函数subset(("A","b","C","D"),3)
,它提供以下输出:
("A","b","C")
("A","b","D")
("A","C","D")
("b","C","D")
我如何在python 3中执行此操作?
答案 0 :(得分:2)
为此目的明确构建了itertools.combinations
函数:
>>> from itertools import combinations
>>> list(combinations(("A","b","C","D"), 3))
[('A', 'b', 'C'), ('A', 'b', 'D'), ('A', 'C', 'D'), ('b', 'C', 'D')]
>>>
来自docs:
itertools.combinations(iterable, r)
从输入
r
返回元素的iterable
长度子序列。