例如,我需要生成从“a”到“]]]]]]”的组合,为了实现这个目的,我使用了这个python脚本。在
import itertools
DATA_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&-
()@=+;/!%$\\'\",.<>*^{}#~_[]"
b = 10
for i in range(1,int(b)+1):
for e in itertools.combinations(DATA_ALPHA_NUM,i): print(''.join(e))
但现在我需要做相反的事情,例如:如果我给新脚本“1”,它将输出“a”,如果我给90,它将输出“]”等。
我写了几个脚本,在不到737191的组合中工作得很好,但之后就不好了。在
编辑:有人写了这样的东西,然后在几乎完美的情况下删除它。。在
^{pr2}$
概述
关键是遍历累积组合,直到达到索引为止。在
解决方案
优化
如果需要考虑速度,则可以构建更快版本的comb()函数。在
一种方法是预先计算阶乘,然后在需要时进行查找:
^{pr2}$还有另一种方法可以完全避免大阶乘,而且不需要辅助存储:
工作原理
首先将组合分解为其组成组:
这意味着,当您一次运行
itertools.combinations('ABCDEFGH', 3)
处理n=8
个字母时,有56个组合。前21个以A
开头,后15个以B
开头,下10个以C
开头,下6个以D
开头,下3个以E
开头,最后1个以F
开头。在假设你想找到56个组合中的第25个。它属于第二组,所以你的第一个字母是
B
。在由于25-21是4,那么您需要在
itertools.combinations('CDEFGH', 2)
定义的“B”组的15个成员中找到第4个组合。重复上述过程,直到所有的字母都被提取出来。在测试
下面是一个测试,以确保它能产生预期的结果:
你不想要组合。的确,你想要“aa”。但对于组合,因为你永远不会选择两次相同的项目,这是不会发生的。在
所以这里有一个“累积积”的正确版本,实际上,就像雷蒙德对组合所做的那样,我必须数数(90,90+90**2,90+90**2+90**3,…)来找出与我所跟踪的组合对应的好的幂。在
请注意,它并没有优化,因为我在分割产品。。。只值一个!在
更新:我添加了一个方法来检索两个索引之间的列表,基于相同的概念,但在本例中,最好使用slice:)
^{pr2}$相关问题 更多 >
编程相关推荐