python非递归全排列实现方法


Posted in Python onApril 10, 2017

刚刚开始学习python,当前看到了函数这一节。结合数组操作,写了个非递归的全排列生成。原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位置插入,生成n+1个新的全排列。因为Python切割数组或者字符串,以及合并比较方便,所以,程序会节省很多代码。

def getArrayInsertCharToStr(STR,CHAR):
  arr =[]
  s_len = len(STR)
  index =0
  while index <= s_len:
    #分割字符串
    arr.append(STR[:index]+CHAR+STR[index:s_len])
    index = index + 1
  return arr  

def getArrayInsertCharToArray(array,CHAR):
  index = 0
  re_array = []
  while index < len(array):
    re_array = re_array + getArrayInsertCharToStr(array[index],CHAR)
    index = index + 1
  return re_array       

def getPermutation(STR):
    resultArr = [STR[0]]
    for item in STR[1:]:
      resultArr = getArrayInsertCharToArray(resultArr,item)
    return   resultArr


print(getPermutation('abc'))

以上这篇python非递归全排列实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
菜鸟使用python实现正则检测密码合法性
Jan 05 Python
利用Python获取赶集网招聘信息前篇
Apr 18 Python
Python制作词云的方法
Jan 03 Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 Python
详谈python3中用for循环删除列表中元素的坑
Apr 19 Python
对Python3 goto 语句的使用方法详解
Feb 16 Python
详解Python3中ceil()函数用法
Feb 19 Python
值得收藏的10道python 面试题
Apr 15 Python
python协程gevent案例 爬取斗鱼图片过程解析
Aug 27 Python
浅谈python的elementtree模块处理中文注意事项
Mar 06 Python
Python基于stuck实现scoket文件传输
Apr 02 Python
Pytest实现setup和teardown的详细使用详解
Apr 17 Python
python 生成器生成杨辉三角的方法(必看)
Apr 10 #Python
Python贪吃蛇游戏编写代码
Oct 26 #Python
OpenCV实现人脸识别
Apr 07 #Python
python使用opencv进行人脸识别
Apr 07 #Python
Python 实现链表实例代码
Apr 07 #Python
python中如何使用朴素贝叶斯算法
Apr 06 #Python
python获取当前运行函数名称的方法实例代码
Apr 06 #Python
You might like
PHP4.04简明安装
2006/10/09 PHP
phpfans留言版用到的数据操作类和分页类
2007/01/04 PHP
php中在PDO中使用事务(Transaction)
2011/05/14 PHP
让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
2011/05/17 PHP
PHP类与对象中的private访问控制的疑问
2012/11/01 PHP
php Xdebug的安装与使用详解
2013/06/20 PHP
php 生成Tab键或逗号分隔的CSV
2016/09/24 PHP
PhpStorm配置Xdebug调试的方法步骤
2019/02/02 PHP
原生javascript实现简单的datagrid数据表格
2015/01/02 Javascript
Javascript中的apply()方法浅析
2015/03/15 Javascript
解决微信浏览器Javascript无法使用window.location.reload()刷新页面
2016/06/21 Javascript
原生态js,鼠标按下后,经过了那些单元格的简单实例
2016/08/11 Javascript
jquery实现垂直和水平菜单导航栏
2020/08/27 Javascript
用原生js做单页应用
2017/01/17 Javascript
前端构建工具之gulp的语法教程
2017/06/12 Javascript
js+html获取系统当前时间
2017/11/10 Javascript
使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件(推荐)
2018/05/01 Javascript
Vue模拟数据,实现路由进入商品详情页面的示例
2018/08/31 Javascript
在vue项目中,将juery设置为全局变量的方法
2018/09/25 Javascript
js实现图片上传到服务器和回显
2020/01/19 Javascript
python定时关机小脚本
2018/06/20 Python
在dataframe两列日期相减并且得到具体的月数实例
2018/07/03 Python
python pyinstaller打包exe报错的解决方法
2019/11/02 Python
细说CSS3中的选择符
2008/10/17 HTML / CSS
香港最新科技与优质家居产品购物网站:J SELECT
2018/08/21 全球购物
葡萄牙航空官方网站:TAP Air Portugal
2019/10/31 全球购物
中科方德软件测试面试题
2016/04/21 面试题
机械工程系毕业生求职信
2013/09/27 职场文书
文秘求职信范文
2014/04/10 职场文书
大学生个人求职信
2014/06/02 职场文书
应聘会计求职信
2014/06/11 职场文书
2015年技术工作总结范文
2015/04/20 职场文书
2015年度招聘工作总结
2015/05/28 职场文书
Pytorch distributed 多卡并行载入模型操作
2021/06/05 Python
简单介绍Python的第三方库yaml
2021/06/18 Python
JVM的类加载器和双亲委派模式你了解吗
2022/03/13 Java/Android