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)]

 

 

########################

posted @   西北逍遥  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2022-03-13 实验数据记录
2021-03-13 IfcScalarTimesVector
2020-03-13 IfcWorkControl
点击右上角即可分享
微信分享提示