Generate all subsets of size k (containing k elements) in Python

前端 未结 3 820
后悔当初
后悔当初 2020-12-03 14:31

I have a set of values and would like to create list of all subsets containing 2 elements.

For example, a source set ([1,2,3]) has the following 2-elem

3条回答
  •  忘掉有多难
    2020-12-03 14:41

    This is a subset of the power set of {1, 2, 3} (or whatever set) containing all two-element sets.

    See the Python itertools documentation and search on the term "powerset" for a general answer to this problem.

提交回复
热议问题