受python itertools启发的序列生成工具。
seqgentools的Python项目详细描述
Seqgentools
Python组合图文库,它通过组合、排列和/或另一序列的乘积生成可索引序列。它在不实际分配内存的情况下创建序列,因此具有大量(可能是无限)元素的序列可以在许多应用中使用,例如最优解搜索问题。
动机
pythonitertools包为用户提供了创建“高效循环迭代器”的能力。从“优化问题”的角度来看,“itertools”可能是一个方便的工具,可以在不实际为空间实例分配内存的情况下,简洁地定义一个可能很大的搜索空间。
例如,下面的代码片段生成一个具有1000个数据点的三维空间:
>>>forx,y,zinitertools.product(range(10),repeat=3):>>># DO work on each "point of (x,y,z)"
但是,itertools有一个关键的缺点,可以用作搜索算法的搜索空间生成器:应该按顺序访问其元素。例如,要访问上一代码示例中(9,9,9)的最后一点,需要遍历(0,0,0)到(9,9,8)之间的所有999个元素。这是因为Python迭代器不支持索引。下一个代码示例显示无法索引迭代器。
>>>space=itertools.product(range(10),repeat=3)>>>space[999]Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:'itertools.product'objectisnotsubscriptable
“SEQANTIOOLS”采用了“itertools”的核心能力,并向它们添加了随机访问索引能力。
>>>importseqgentoolsasseq>>>space=seq.Product(range(10),repeat=3)>>>space[999](9,9,9)
安装
“seqgentools”可以使用“pip”轻松安装,如下所示。
>>> pip install seqgentools --user
要访问最新功能,请使用git从该存储库下载。
>>> git clone https://github.com/NCAR/seqgentools.git
开始
只要可能,“seqgentools”就遵循使用“itertools”的约定,这样用户就可以利用他们关于“itertools”的知识。如果你不熟悉“itertools”,我相信,投资几分钟看看它能为你做些什么是值得的。
做就是相信:请按照下面的例子来了解“seqgentools”是如何工作的。
>>>importseqgentoolsasseq>>>>>>###### Product #######>>>>>>prod=seq.Product(range(2),range(2))>>>list(prod)[(0,0),(0,1),(1,0),(1,1)]>>>prod[3](1,1)>>>>>>###### Permutations #######>>>>>>perm=seq.Permutations("ABC",2)>>>list(perm)[('A','B'),('A','C'),('B','A'),('B','C'),('C','A'),('C','B')]>>>perm[3]('B','C')>>>>>>###### Combinations #######>>>>>>comb=seq.Combinations("ABC",2)>>>list(comb)[('A','B'),('A','C'),('B','C')]>>>comb[2]('B','C')>>>>>>###### Combinations_with_replacement #######>>>>>>combr=seq.Combinations_with_replacement("ABC",2)>>>list(combr)[('A','A'),('A','B'),('A','C'),('B','B'),('B','C'),('C','C')]>>>combr[2]('A','C')>>>>>>###### PermutationRange #######>>>>>>permrange=seq.PermutationRange("ABC")>>>list(permrange)[(),('A',),('B',),('C',),('A','B'),('A','C'),('B','A'),('B','C'),('C','A'),('C','B'),('A','B','C'),('A','C','B'),('B','A','C'),('B','C','A'),('C','A','B'),('C','B','A')]>>>permrange[3]('C',)>>>>>>###### CombinationRange #######>>>>>>combrange=seq.CombinationRange("ABC")>>>list(combrange)[(),('A',),('B',),('C',),('A','B'),('A','C'),('B','C'),('A','B','C')]>>>combrange[2]('B',)
搜索空间生成器
- “seqgentools”包含一个分层搜索空间生成器,可以显著减少
- 与每个搜索维度的原始产品相比,搜索空间的总大小。
API文档
正在开发中。
在api文档准备就绪之前,请尽可能将“itertools”api文档作为“seqgentools”在“itertools”api之后。
在这个版本中,“seqgentools”实现了以下序列生成器。
- Count: generates a sequence of, possibily infinite, evenly spaced numbers
- Cycle: generates a cyclic chain of another sequence
- Repeat: generates a repeating sequece of object
- Chain: generates a chained sequence of another sequences
- Product: generates a sequence of mathematical product of another sequences
- Permutations: generates a permuted sequence of another sequence
- Combinations: generates a combinated sequence of another sequence
- Combinations_with_replacement: generates a combinated sequence of another sequence with replacement
- PermutationRange: generates a chained sequence of series of permuted sequence
- ranging r=0 to r=n of another sequence
- CombinationRange: generates a chained sequence of series of combinated sequence
- ranging r=0 to r=n of another sequence
- Wrapper: generates a sequence from Python sequece data types
- Fibonacci: generates an random-accesible Fibonacci sequence
[备注]
- “seqgentools” supports randomly accessible indexing of infinite sequences.
- “Product”, “Permutations”, “Combinations”, “Combinations_with_replacement”, “PermutationRange”, and “CombinationRange” do not accept infinite sequence as their input(s).
- test codes in “tests” subdirectory could be a good place to start further investigation.
- “Wrapper” sequence generator wraps Python sequence data types such as list, tuple, dictionary, string, set, etc.
- The name of sequence generators in “seqgentools” starts with a capital letter while “itertools” starts with a lower-case. This is to emphasize that sequence generators are instantiated from class, not from function.