get all the partitions of the set python with itertools
问题 How to get all partitions of a set? For example, I have array [1, 2, 3] . I need to get [[1], [2], [3]], [[1], [2, 3]], [[2], [1,3]], [[3], [1, 2]], [[1, 2, 3]] . Now, I wrote this code: def neclusters(S, K): for splits in itertools.combinations(range(len(S)), K): yield np.split(S, 1 + np.array(splits)) But that code don't return [[2],[1,3]] . I could take all permutations of the original set and run this code on them. But can this be made easier? 回答1: I wrote this one for fun: def partition