Python排列组合问题
1.字符串的全排列
问题描述:打印出原字符串中所有字符的所有排列。——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次。
Python可以用生成器解决:
def permutation(elements):
if len(elements) <=1:
yield elements
else:
for perm in permutation(elements[1:]):
for i in range(len(elements)):
yield perm[:i] + elements[0:1] + perm[i:] if __name__ == "__main__":
s='Diei'
for item in list(permutation(list(s))):
print ''.join(item)
——生成器方法就是不停的插入前面一个元素的过程。
2.字符串的全组合
问题描述:打印出原字符串中所有字符的所有可能的组合。组合长度范围在1到字符串长度之间。
import copy def combine(l, n):
if n==0:
return
answers = []
one = [0 for i in range(n)]
def next_c(li = 0, ni = 0):
if ni == n:
answers.append(copy.copy(one))
return
for lj in xrange(li, len(l)):
one[ni] = l[lj]
next_c(lj + 1, ni + 1)
next_c()
return answers if __name__ == "__main__":
s='Dieicd'
l=list(set(s))
answer=[]
for i in range(len(l)):
answer+=combine(l,i+1)
print len(answer)
for i in answer:
print ''.join(i)
——按包含的字符来检测
3.使用Python的itertools库
from itertools import product
#两个序列对应的排列
from itertools import permutations
#排列
from itertools import combinations
#组合
l = [1, 2, 3]
print len(list(product(l,repeat=3)))
#print list(product(l, repeat=4))
print list(permutations(l))
print list(combinations([1,2,3,4,5], 3))
Python排列组合问题的更多相关文章
- python排列组合之itertools模块
1. 参考 几个有用的python函数 (笛卡尔积, 排列, 组合) 9.7. itertools — Functions creating iterators for efficient loopi ...
- Python排列组合实验
import itertools 排列: 4个数内选2个 >>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), ( ...
- python 排列组合
笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...
- Python排列组合
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- 【Python】排列组合itertools & 集合set
■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...
- 排列组合python
python 的 itertools模块 可以专业的处理的排列组合问题 写在自己博客里,怕下次找不到喽
- python 实现排列组合
1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...
- python自带的排列组合函数
需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯. 分析:提炼 ...
- python编写排列组合,密码生产功能
python编写排列组合 python在编写排列组合是会用到 itertools 模块 排列 import itertools mylist = list(itertools.permutation ...
随机推荐
- LinQ高级查询
1.模糊查询 con.Users.Where(a =>a.UserName.Contains(name)).ToList(); //包含name con.Users.Where(a =>a ...
- 循序渐进Python3(十一) --1-- web之css
css样式: css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化,CSS的可以使页面更加的美观. 基本上所有的html页面都或多或少的使用css. ...
- NSURLCache详解和使用
使用缓存的目的是为了使应用程序能更快速的响应用户输入,是程序高效的运行.有时候我们需要将远程web服务器获取的数据缓存起来,以空间换取时间,减少对同一个url多次请求,减轻服务器的压力,优化客户端网络 ...
- The user specified as a definer ('root'@'%') does not exist
The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 解决方法: 2.进入mysql ...
- 自定义shape文件
1.shape文件 btn_bg.xml文件内容 <?xml version="1.0" encoding="utf-8"?> <shape ...
- Expected one result (or null) to be returned by selectOne(), but found 2
这个问题在于你查询sql返回结果是多个值.一个集合,但是你在service的实现层的dao都调用了.get方法.而是应该使用.getlist方法等.
- Redis 缓存 + Spring 的集成示例
参考网址:http://blog.csdn.net/defonds/article/details/48716161
- linux 登录档配置分析
登录档的重要性 解决系统方面的错误: 解决网络服务的问题: 过往事件记录簿: Linux 常见的登录档档名 /var/log/cron: 你的 crontab 排程有没有实际被进行? 进行过程有没有发 ...
- ORACLE 数据库需要创建索引的规则
1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: ...
- 内核对TCP REUSEPORT的优化
Q&A 当有人问起我关于reuseport的一些事的时候,我们的对话基本如下:Q1:什么是reuseport?A1:reuseport是一种套接字复用机制,它允许你将多个套接字bind在同一个 ...