与限产子组合组合

2023-09-11 23:17:53 作者:そ偂哖舊客

我正在与蟒蛇3.我用的工作如下功能:

I'm working with python 3. The function I'm working with is as follows:

def sub_combinations(segment):
  if len(segment) == 1:
    yield (segment,)
  else:
    for j in sub_combinations(segment[1:]):
      yield ((segment[0],),)+j
      for k in range(len(j)):
         yield (((segment[0],)+j[k]),) + (j[:k]) +(j[k+1:]) 

它的一个版本,这个功能:

its a version of this function:

屈服子组合

的输出是如下的(1,2,3,4,5):

the output is as follows for (1,2,3,4,5):

((1,), (2,), (3,), (4,), (5,))
((1, 2), (3,), (4,), (5,))
((1, 3), (2,), (4,), (5,))
((1, 4), (2,), (3,), (5,)) *
((1, 5), (2,), (3,), (4,)) *
((1,), (2, 3), (4,), (5,))
((1, 2, 3), (4,), (5,))
((1, 4), (2, 3), (5,)) *
((1, 5), (2, 3), (4,)) *
((1,), (2, 4), (3,), (5,))
((1, 2, 4), (3,), (5,))
((1, 3), (2, 4), (5,))
((1, 5), (2, 4), (3,)) *
((1,), (2, 5), (3,), (4,)) *
((1, 2, 5), (3,), (4,)) *
((1, 3), (2, 5), (4,)) *
((1, 4), (2, 5), (3,)) *
((1,), (2,), (3, 4), (5,))
((1, 2), (3, 4), (5,))
((1, 3, 4), (2,), (5,))
((1, 5), (2,), (3, 4)) *
((1,), (2, 3, 4), (5,))
((1, 2, 3, 4), (5,))
((1, 5), (2, 3, 4)) *
((1,), (2, 5), (3, 4)) *
((1, 2, 5), (3, 4)) *
((1, 3, 4), (2, 5)) *
((1,), (2,), (3, 5), (4,))
((1, 2), (3, 5), (4,))
((1, 3, 5), (2,), (4,))
((1, 4), (2,), (3, 5)) *
((1,), (2, 3, 5), (4,))
((1, 2, 3, 5), (4,))
((1, 4), (2, 3, 5)) *
((1,), (2, 4), (3, 5))
((1, 2, 4), (3, 5))
((1, 3, 5), (2, 4))
((1,), (2,), (3,), (4, 5))
((1, 2), (3,), (4, 5))
((1, 3), (2,), (4, 5))
((1, 4, 5), (2,), (3,)) *
((1,), (2, 3), (4, 5))
((1, 2, 3), (4, 5))
((1, 4, 5), (2, 3)) *
((1,), (2, 4, 5), (3,))
((1, 2, 4, 5), (3,))
((1, 3), (2, 4, 5))
((1,), (2,), (3, 4, 5))
((1, 2), (3, 4, 5))
((1, 3, 4, 5), (2,))
((1,), (2, 3, 4, 5))
((1, 2, 3, 4, 5),)

现在的问题是,如果我有更大的元组的工作,该函数sub_combinations返回一个庞大的数据量和时间过长计算它。为了解决这个问题,我想限制通过添加一个额外的参数返回的数据的量。例如,sub_combinations((1,2,3,4,5),2)应返回的数据上面,但没有元组标有星号。这些都将被丢弃,因为在元组consequtive值之间的偏移是含有大于2。例如,行(1,4),(1,5)或(2,5)和的同类(1,2,5)等,都将被丢弃。

The problem is that if I work with larger tuples, the function sub_combinations returns a huge amount of data and takes too long to compute it. To address this, I want to limit the amount of data returned by adding an extra argument. For example, sub_combinations((1,2,3,4,5), 2) should return the data above but without the tuples marked with a star. These are dropped because the offset between consequtive values in the tuple is greater than 2. For example, rows containing (1, 4), (1, 5) or (2, 5) and the likes of (1, 2, 5) etc, are dropped.

for k in range(len(j))

需要进行调整,以删除这些行,但我还没有想出尚未方式。有什么建议?

needs to be adjusted to drop these lines, but I haven't figured out yet how. Any suggestions?

巴里

推荐答案

我觉得下面的变化导致您正在寻找的输出:

I think the following change results in the output you are looking for:

def sub_combinations(segment, max_offset=None):
   data = tuple([e] for e in segment)
   def _sub_combinations(segment):
      if len(segment) == 1:
         yield (segment,)
      else:
         for j in _sub_combinations(segment[1:]):
            yield ((segment[0],),)+j
            for k in range(len(j)):
               if max_offset and data.index(j[k][0]) - data.index(segment[0]) > max_offset:
                  break
               yield (((segment[0],)+j[k]),) + (j[:k]) +(j[k+1:])
   for combination in _sub_combinations(data):
      yield tuple(tuple(e[0] for e in t) for t in combination)

这里的想法是,你打破得到一个元组,将有出的 K 循环,而不是一个偏移量小于 max_offset 。

The idea here is that you break out of the k loop instead of yielding a tuple that would have an offset larger than max_offset.