How to find all partitions of a list S into k subsets (can be empty)?
问题 I have a list of unique elements, let's say [1,2], and I want to split it onto k=2 sublists. Now I want to have all possible sublists: [ [ [1,2],[] ], [ [1],[2] ], [ [2],[1] ], [ [],[1,2] ] ] And I want to split onto 1<=k<=n sublists, so for k=1 it will be: [ [1, 2] ] How can I do that with Python 3? UPDATE: my goal is to get all possible partitions of list of N unique numbers, where each partition will have k sublists. I would like to show better example than I have shown upper, I hope I