Python中的快速组合生成器

Python中的快速组合生成器,python,performance,generator,combinatorics,Python,Performance,Generator,Combinatorics,作为python大型项目的一部分,我需要一个快速生成器函数,该函数生成所有可能的小于n的非负整数集,这样每个集合最多包含s元素,并且集合中最大和最小数之间的差值小于w 到目前为止,我实现的最快的实现是使用itertools: import itertools def subsample(n, s, w): nn = range(w) for p in range(s): o = list(itertools.combinations(nn, p+1))

作为python大型项目的一部分,我需要一个快速生成器函数,该函数生成所有可能的小于
n
的非负整数集,这样每个集合最多包含
s
元素,并且集合中最大和最小数之间的差值小于
w

到目前为止,我实现的最快的实现是使用
itertools

import itertools

def subsample(n, s, w):
    nn = range(w)
    for p in range(s):
        o = list(itertools.combinations(nn, p+1))
        for t in o:
            yield t
        for _ in range(0, n-w):
            pt = o
            o = [tuple([op + 1 for op in list(u)]) for u in pt]
            for t in list((set(o) ^ set(pt)) & set(o)):
                yield t
例如:

In [1]: list(subsample(6,3,3))
Out [1]: [(0,), (1,), (2,), (3,), (4,), (5,), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (2, 4), (4, 5), (3, 5), (0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

但我相信,必须有更有效的方法来做到这一点。有什么能让这更快吗?

这里有一些基于Knuth算法的生成器

  • subset4
    生成具有k个或更少元素的1..n的所有子集
  • subset5
    subset4
    生成的子集限制为最大差值为w的子集
  • 子集
    生成长度正好为k的1..n的所有子集
  • 子集2
    子集
    生成的
运行
megatest()
函数,测试
子项5
生成器的多个n、k和w值

#1..n的k个或更少元素的所有子集
def子条款4(n,k):
a=[0]*k
i=0
当i>=0时:
a[i]+=1
收益率a[0:i+1]
r=a[i]+1
i+=1
当i=n+1:i-=1
r=a[i]
a[i]+=1
j=2
i+=1
而我w:返回
a=范围(1,k+1)
而a[0]=0和(a[i]+k-i>=n+1或a[i]+k-i-a[0]>w):i-=1
r=a[i]
a[i]+=1
j=2
i+=1
而我s1=[s表示子集合4(n,k)中的s,如果s[-1]-s[0]最快的方法是什么?这是一种不好的提问方式。更容易得到答案的是,有没有什么东西可以让它更快?除非你想破坏你程序的意图,否则我不会回答这个问题。你的python版本是什么?你使用的是哪种python版本?这可能与关于range;@A.J.如果问题不好,他会道歉。我根据你的建议编辑了它。@Kasra和@Eithos,我使用的是python 2.7。
range
vs
xrange
问题是一个很好的观察,谢谢。@STU如果你把你的问题发过来,你可能会得到一些更好的答案。只是一个建议。嘿,this非常好,比我的实现快3倍多:
%timeit[f代表子集合5(500,6,11)中的f]
给出了
474ms/循环
,而
%timeit[f代表子样本(500,6,12)]
给出了
1.71s/循环
。注意参数
w
的约定略有不同。
# all subsets of k or less elements of 1..n
def subsets4(n,k):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n:
      i -= 1

# all subsets of k or less elements of 1..n with max difference <= w
def subsets5(n,k,w):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n and r-a[0] <= w:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n or a[i]+1-a[0] > w:
      i -= 1

# all subsets of 1..n having exactly k elements
def subsets(n,k):
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    # find i
    i = k-1
    while i >= 0 and a[i]+k-i >= n+1: i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

# all subsets of 1..n having exactly k elements and whose max
# difference is w
def subsets2(n,k,w):
  if k > w: return
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    i = k-1
    while i >= 0 and (a[i]+k-i >= n+1 or a[i]+k-i-a[0] > w) : i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

def test(n,k,w):
  s1 = [ s for s in subsets4(n,k) if s[-1] - s[0] <= w ]
  s2 = [ s for s in subsets5(n,k,w) ]
  if s1 == s2:
    print "OK", n,k,w
    return 0
  else:
    print "NOT OK", n, k, w
    return 1 

# for s in subsets2(10,3,4): print s
# for s in subsets(10,3): print s
def megatest():
  failed = 0
  for n in xrange(10,20):
    for k in xrange(1,n+1):
      for w in xrange(k,n+1):
        failed += test(n,k,w)
  print "failed:", failed