慕哥9229398
虽然这个答案很好,但我还是建议以下skovorodkin的答案:>>> def partition(number):... answer = set()... answer.add((number, ))... for x in range(1, number):... for y in partition(number - x):... answer.add(tuple(sorted((x, ) + y)))... return answer... >>> partition(4)set([(1, 3), (2, 2), (1, 1, 2), (1, 1, 1, 1), (4,)])如果要所有排列(即(1,3)和(3,1))更改answer.add(tuple(sorted((x, ) + y))为answer.add((x, ) + y)