python 全排列
python 全排列
import itertools # 利用itertools库中的permutations函数,给定一个排列,输出他的全排列 def all_permutation_fun(permutation): # itertools.permutations返回的只是一个对象,需要将其转化成list # 每一种排列情况以元组类型存储 all_permutation = list(itertools.permutations(permutation)) return all_permutation permutation = [1,2,5,4] all_permutation_result = all_permutation_fun(permutation) print(all_permutation_result)
输出结果:
[(1, 2, 5, 4), (1, 2, 4, 5), (1, 5, 2, 4), (1, 5, 4, 2), (1, 4, 2, 5), (1, 4, 5, 2), (2, 1, 5, 4), (2, 1, 4, 5), (2, 5, 1, 4), (2, 5, 4, 1), (2, 4, 1, 5), (2, 4, 5, 1), (5, 1, 2, 4), (5, 1, 4, 2), (5, 2, 1, 4), (5, 2, 4, 1), (5, 4, 1, 2), (5, 4, 2, 1), (4, 1, 2, 5), (4, 1, 5, 2), (4, 2, 1, 5), (4, 2, 5, 1), (4, 5, 1, 2), (4, 5, 2, 1)]
########################
QQ 3087438119