Python实现全排列的打印


Posted in Python onAugust 18, 2018

本文为大家分享了Python实现全排列的打印的代码,供大家参考,具体如下

问题:输入一个数字:3,打印它的全排列组合:123 132 213 231 312 321,并进行统计个数。

下面是Python的实现代码:

#!/usr/bin/env python
# -*- coding: <encoding name> -*- 
'''
全排列的demo
input : 3
output:123 132 213 231 312 321
'''
 
total = 0
 
def permutationCove(startIndex, n, numList):
  '''递归实现交换其中的两个。一直循环下去,直至startIndex == n
  '''
  global total
  if startIndex >= n:
    total += 1
    print numList
    return
    
  for item in range(startIndex, n):
    numList[startIndex], numList[item] = numList[item], numList[startIndex]
    permutationCove(startIndex + 1, n, numList )
    numList[startIndex], numList[item] = numList[item], numList[startIndex]
      
 
n = int(raw_input("please input your number:"))
startIndex = 0
total = 0
numList = [x for x in range(1,n+1)]
print '*' * 20
for item in range(0, n):
  numList[startIndex], numList[item] = numList[item], numList[startIndex]
  permutationCove(startIndex + 1, n, numList)
  numList[startIndex], numList[item] = numList[item], numList[startIndex]
 
print total

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
ssh批量登录并执行命令的python实现代码
May 25 Python
教你安装python Django(图文)
Nov 04 Python
Python最基本的输入输出详解
Apr 25 Python
python 数据的清理行为实例详解
Jul 12 Python
Python实现按学生年龄排序的实际问题详解
Aug 29 Python
python删除某个字符
Mar 19 Python
基于Python对数据shape的常见操作详解
Dec 25 Python
Django logging配置及使用详解
Jul 23 Python
Form表单及django的form表单的补充
Jul 25 Python
Django 实现前端图片压缩功能的方法
Aug 07 Python
详解python tkinter 图片插入问题
Sep 03 Python
python3操作redis实现List列表实例
Aug 04 Python
python递归实现快速排序
Aug 18 #Python
pyqt5的QWebEngineView 使用模板的方法
Aug 18 #Python
python递归全排列实现方法
Aug 18 #Python
python使用PIL给图片添加文字生成海报示例
Aug 17 #Python
Python在for循环中更改list值的方法【推荐】
Aug 17 #Python
Python简单读写Xls格式文档的方法示例
Aug 17 #Python
Python实现的连接mssql数据库操作示例
Aug 17 #Python
You might like
PHP与SQL注入攻击[一]
2007/04/17 PHP
php表单转换textarea换行符的方法
2010/09/10 PHP
php 解压rar文件及zip文件的方法
2014/05/05 PHP
php使用fgetcsv读取csv文件出现乱码的解决方法
2014/11/08 PHP
PHP+MYSQL中文乱码问题
2015/07/01 PHP
PHP不使用递归的无限级分类简单实例
2016/11/05 PHP
ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解
2019/04/09 PHP
jQuery 1.2.x 升? 1.3.x 注意事项
2009/05/06 Javascript
写给想学习Javascript的朋友一点学习经验小结
2010/11/23 Javascript
AngularJS入门教程中SQL实例详解
2016/07/27 Javascript
JS实现图片剪裁并预览效果
2016/08/12 Javascript
AngularGauge 属性解析详解
2016/09/06 Javascript
Node.js读写文件之批量替换图片的实现方法
2016/09/07 Javascript
JS简单生成随机数(随机密码)的方法
2017/05/11 Javascript
基于easyui checkbox 的一些操作处理方法
2017/07/10 Javascript
原生nodejs使用websocket代码分享
2018/04/07 NodeJs
使用JQuery自动完成插件Auto Complete详解
2019/06/18 jQuery
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
Javascript confirm多种使用方法解析
2020/09/25 Javascript
[01:05:40]VG vs Newbee 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
[01:04:14]VP vs TNC 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
Python的函数嵌套的使用方法
2014/01/24 Python
python基础教程之缩进介绍
2014/08/29 Python
Python中使用插入排序算法的简单分析与代码示例
2016/05/04 Python
python实现装饰器、描述符
2018/02/28 Python
numpy使用fromstring创建矩阵的实例
2018/06/15 Python
详解python 模拟豆瓣登录(豆瓣6.0)
2019/04/18 Python
Python CSV文件模块的使用案例分析
2019/12/21 Python
有关环保的标语
2014/06/13 职场文书
项目经理助理岗位职责
2015/04/13 职场文书
运动会通讯稿100字
2015/07/20 职场文书
2016年感恩教师节校园广播稿
2015/12/18 职场文书
Python中for后接else的语法使用
2021/05/18 Python
python opencv检测直线 cv2.HoughLinesP的实现
2021/06/18 Python
小喇叭开始广播了! 四十多年前珍贵老照片
2022/05/09 无线电
SpringBoot接入钉钉自定义机器人预警通知
2022/07/15 Java/Android