1、递归
def perm(n, begin, end): if begin >= end: print(n) else: i = begin for num in range(begin, end): n[num], n[i] = n[i], n[num] perm(n, begin + 1, end) n[num], n[i] = n[i], n[num] n = [1, 2, 3] perm(n, 0, len(n))
2、非递归
import itertools print list(itertools.permutations([1, 2, 3, 4],4)) #第二个参数表示,4个数的全排列