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 相关文章推荐
Python实现二分法算法实例
Feb 02 Python
Python批量转换文件编码格式
May 17 Python
Python聚类算法之基本K均值实例详解
Nov 20 Python
使用Python的Scrapy框架十分钟爬取美女图
Dec 26 Python
python实现m3u8格式转换为mp4视频格式
Feb 28 Python
python如何拆分含有多种分隔符的字符串
Mar 20 Python
python定时关机小脚本
Jun 20 Python
python TKinter获取文本框内容的方法
Oct 11 Python
深入浅析Python 中 is 语法带来的误解
May 07 Python
详解从Django Allauth中进行登录改造小结
Dec 18 Python
python sorted函数原理解析及练习
Feb 10 Python
基于python制作简易版学生信息管理系统
Apr 20 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程序的国际化实现方法(利用gettext)
2011/08/14 PHP
XAMPP安装与使用方法详细解析
2013/11/27 PHP
PHP实现自动对图片进行滚动显示的方法
2015/03/12 PHP
ubuntu下配置nginx+php+mysql详解
2015/09/10 PHP
自定义min版smarty模板引擎MinSmarty.class.php文件及用法
2016/05/20 PHP
java解析json方法总结
2019/05/16 PHP
php使用socket调用http和smtp协议实例小结
2019/07/26 PHP
Javascript 学习书 推荐
2009/06/13 Javascript
jquery判断浏览器类型的代码
2012/11/05 Javascript
JS计算网页停留时间代码
2014/04/28 Javascript
jquery代码实现多选、不同分享功能
2015/07/31 Javascript
Jquery ajax加载等待执行结束再继续执行下面代码操作
2015/11/24 Javascript
AngularJS实践之使用NgModelController进行数据绑定
2016/10/08 Javascript
js微信分享实现代码
2020/10/11 Javascript
Angular搜索 过滤 批量删除 添加 表单验证功能集锦(实例代码)
2017/10/25 Javascript
在vue中使用express-mock搭建mock服务的方法
2018/11/07 Javascript
移动端手指操控左右滑动的菜单
2019/09/08 Javascript
使用Vue实现简单计算器
2020/02/25 Javascript
uniapp,微信小程序中使用 MQTT的问题
2020/07/11 Javascript
详解javascript脚本何时会被执行
2021/02/05 Javascript
跟老齐学Python之再深点,更懂list
2014/09/20 Python
用Python编写简单的定时器的方法
2015/05/02 Python
Python中基本的日期时间处理的学习教程
2015/10/16 Python
详解多线程Django程序耗尽数据库连接的问题
2018/10/08 Python
Python创建一个空的dataframe,并循环赋值的方法
2018/11/08 Python
Python按照list dict key进行排序过程解析
2020/04/04 Python
Pycharm IDE的安装和使用教程详解
2020/04/30 Python
win10安装python3.6的常见问题
2020/07/01 Python
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
银行学习十八大感想
2014/01/11 职场文书
优秀女职工事迹材料
2014/02/06 职场文书
2014年综合治理工作总结
2014/11/20 职场文书
新生入学欢迎词
2015/01/26 职场文书
2016教师政治学习心得体会
2016/01/23 职场文书
mongoDB数据库索引快速入门指南
2022/03/23 MongoDB
Oracle 11g数据库使用expdp每周进行数据备份并上传到备份服务器
2022/06/28 Oracle