While this answer is fine, I'd recommend skovorodkin's answer below:
>>> 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,)])
If you want all permutations(ie (1, 3) and (3, 1)) change answer.add(tuple(sorted((x, ) + y))
to answer.add((x, ) + y)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…