l = ['a', 'b', 'c'] def f(lt, start, end): if start == end: print(lt) else: for i in range(start, end+1): lt[start], lt[i] = lt[i], lt[start] f(lt, start+1, end) lt[start], lt[i] = lt[i], lt[start] if __name__ == '__main__': f(l, 0, 2)
运行结果:
l = ['a', 'b', 'c'] def f(lt, start, end): if start == end: print(lt) else: for i in range(start, end+1): lt[start], lt[i] = lt[i], lt[start] f(lt, start+1, end) lt[start], lt[i] = lt[i], lt[start] if __name__ == '__main__': f(l, 0, 2)
运行结果: