<p>首先,我想指出,您正在研究<a href="https://en.wikipedia.org/wiki/Bell_number" rel="nofollow">Bell numbers</a>,这可能会在您完成所有子集的生成之后,简化下一部分的工作。例如,很容易知道每个铃声集的大小;OEIS已经有了<a href="http://oeis.org/A000110" rel="nofollow">the sequence of Bell numbers</a>。在</p>
<p>我手工编写循环以生成铃声集;以下是我的代码:</p>
<pre class="lang-py prettyprint-override"><code>cache = {0: (), 1: ((set([1]),),)}
def bell(x):
# Change these lines to alter memoization.
if x in cache:
return cache[x]
previous = bell(x - 1)
new = []
for sets in previous:
r = []
for mark in range(len(sets)):
l = [s | set([x]) if i == mark else s for i, s in enumerate(sets)]
r.append(tuple(l))
new.extend(r)
new.append(sets + (set([x]),))
cache[x] = tuple(new)
return new
</code></pre>
<p>为了实用起见,我在这里写了一些回忆录。但是,通过注释掉一些代码,并编写一些其他代码,您可以获得以下未记下来的版本,我将其用于基准测试:</p>
^{pr2}$
<p>我的数字是基于一个有几年历史的Thinkpad,我大部分的工作都是在这个平台上完成的。大多数较小的案例速度太快,无法可靠地测量(前几次试验甚至没有一毫秒),所以我的基准测试是<code>bell(9)</code>到{<cd2>}。在</p>
<p>使用标准<code>timeit</code>模块的cpython2.7.11基准测试:</p>
^{3}$
<p>在Pypy4.0.1上,也使用<code>timeit</code>:</p>
<pre><code>$ pypy -mtimeit -s 'from derp import bell' 'bell(9)'
100 loops, best of 3: 14.3 msec per loop
$ pypy -mtimeit -s 'from derp import bell' 'bell(10)'
10 loops, best of 3: 90.8 msec per loop
$ pypy -mtimeit -s 'from derp import bell' 'bell(11)'
10 loops, best of 3: 675 msec per loop
</code></pre>
<p>所以,我得出的结论是,当你试图在它的习惯用法之外使用{<cd5>}时,它的速度不是很快。Bell数是有趣的组合,但它们并不是由我能找到的任何简单的<code>itertools</code>小部件组成的。在</p>
<p>为了回应你最初的问题:打开代码就可以了。希望这有帮助!在</p>
<p>~C</p>