python 生成排列、组合以及选择
from <python cookbook> 19.15
任务
需要对一个序列的排列(permutation)、组合(combination)或选择(selection)进行迭代操作。即使初始的序列长度并不长,组合计算的规则却显示生成的序列可能非常庞大,比如一个长度为13的序列有超过60亿种可能的排列。所以,你肯定不希望在开始迭代前计算并生成序列中的所有项
解决方案
生成器允许你在迭代的时候一次一个的计算需要的对象。如果有很多这种对象,而且你也必须逐个的检查他们,那么程序无可避免的会用很长时间才能完成循环。但至少你不用浪费很多内存来保存所有项:
def _combinators(_handle, items, n):
''' 抽取下列组合的通用结构'''
if n == 0:
yield [ ]
for i, item in enumerate(items):
this_one = [item]
for cc in _combinators(_handle, _handle(items, i), n-1):
yield this_one + cc def combinations(items, n):
''' 取得n个不同的项, 顺序是有意义的'''
def skipIthItem(items, i):
return items[:i] + items[i + 1:]
return _combinators(skipIthItem, items, n) def uniqueCombinations(items, n):
'''取得n个不同的项,顺序无关'''
def afterIthItem(items, i):
return items[i+1:]
return _combinators(afterIthItem, items, n) def selections(items, n):
'''取得n项(不一定要不同),顺序是有意义的'''
def keepAllItems(items, i):
return items
return _combinators(keepAllItems, items, n) def permutations(items):
''' 取得所有项, 顺序是有意义的'''
return combinations(items, len(items)) if __name__ == '__main__':
print "Permutations of 'bar'"
print map(''.join, permutations('bar'))
#输出: ['bar', 'bra', 'abr', 'arb', 'rba', 'rab'] print "Combinations of 2 letters from 'bar'"
print map(''.join, combinations('bar', 2))
# 输出: ['ba', 'br', 'ab', 'ar', 'rb', 'ra'] print "Unique Combinations of 2 letters from 'bar'"
print map(''.join, uniqueCombinations('bar', 2))
# 输出: ['ba', 'br', 'ar'] print "Selections of 2 letters from 'bar'"
print [''.join, selections('bar', 2)]
# 输出: ['bb', 'ba', 'br', 'ab', 'aa', 'ar', 'rb', 'ra', 'rr']
python 生成排列、组合以及选择的更多相关文章
- python编写排列组合,密码生产功能
python编写排列组合 python在编写排列组合是会用到 itertools 模块 排列 import itertools mylist = list(itertools.permutation ...
- 【Python】排列组合itertools & 集合set
■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...
- python 实现排列组合
1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...
- python 编写排列组合
python在编写排列组合是会用到 itertools 模块 排列 import itertools mylist = list(itertools.permutations([)) # 全排列 p ...
- Python实现排列组合
# -*- coding: utf-8 -*-"""Created on Sat Jun 30 11:49:56 2018 @author: zhen"&quo ...
- python算法-排列组合
排列组合 一.递归 1.自己调用自己 2.找到一个退出的条件 二.全排列:针对给定的一组数据,给出包含所有数据的排列的组合 1:1 1,2:[[1,2],[2,1]] 1,2,3:[[1,2,3],[ ...
- python之排列组合测试
# test permutations and combinations import itertools as it for i in it.combinations('abcd',2): prin ...
- python解决排列组合
笛卡尔积:itertools.product(*iterables[, repeat]) import itertools for i in itertools.product('BCDEF', re ...
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
随机推荐
- [Effective C++ --020]宁以pass-by-reference-to-const替换pass-by-value
前言: 我们都用过C的值传递方式,那么在C++情况下,值传递是怎样工作的呢? 比如: int foo(int x); int i; foo(i); 1.程序内部先取得i的一个副本 2.将副本传递到fo ...
- 【转】如何在Mac上撰寫C++程式
原文: http://www.macuknow.com/node/4901 本文使用的开发环境:Xcode 5.其实步骤很简单,只需要简单的几步就ok了. 點選Create a new Xcode p ...
- 命令行一键清除IE记录
清除Internet临时文件 RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 清除Cookies RunDll32.exe InetCpl.cpl, ...
- 20 Best Drag and Drop jQuery Plugins--reference
reference from:http://dizyne.net/20-best-drag-drop-jquery-plugins/ jQuery has done a great job repla ...
- ios代理设计模式
代理设计模式的作用: 1.A对象监听B对象的一些行为,A成为B的代理 2.B对象想告诉A对象一些事情,A成为B的代理 代理设计模式的总结: 如果你想监听别人的一些行为,那么 ...
- [Doc ID 433386.1]JSP Pages Hanging in R12 After Removing Cached Class Files in _pages
In this Document Symptoms Changes Cause Solution References Applies to: Oracle Application ...
- centos install shutter (How to enable Nux Dextop repository on CentOS or RHEL)
http://ask.xmodulo.com/enable-nux-dextop-repository-centos-rhel.html Question: I would like to insta ...
- 【转】BUG敏感度的培养
在我们刚踏入软件测试行业时,不管你是专业的.非专业的,培训出来的还是未培训的.刚进公司时你看着身边的同时报的Bug很多并且大都是严重程度高,自己也很想提高一下,想要提高自己的bug敏感度,建议从下面几 ...
- 20160502-struts2入门--ognl表达式
一.OGNL表达式语言 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语言 ...
- Android开发常见问题及解决方法
http://blog.csdn.net/silangquan/article/details/8104414