句子中单词的递归排列

Recursive permutation of words in a sentence

我想 recursively 获得句子中单词的排列,使相邻单词从左到右以两个为一组。

举个例子,如果我认为 a, B, c, D 是 4 个词,主句中这 4 个词出现了 5 次,如:

主句:a + B + c + a + D

我会得到四个句子作为

c + a + B + c + a
a + B + c + a + D
a + B + c + a + B
B + c + a + B + c

都和主句一样长,需要注意的是主句的最后一个字D只出现一次,而且只出现在[=15之后的句末=] 因为在主句中没有跟在后面的词。

您可以使用带递归的生成器:

s = ['a', 'B', 'c', 'a', 'D']
def combinations(d, _c = []):
  if len(_c) == len(d)+1:
     yield _c
  else:
     for i in d:
       if not _c or any(s[c] == _c[-1] and s[c+1] == i for c in range(len(s)-1)):
          for k in combinations(d, _c+[i]):
            yield k

print('\n'.join(' + '.join(i) for i in combinations(set(s))))

输出:

a + B + c + a + B
a + B + c + a + D
B + c + a + B + c
c + a + B + c + a

您可以使用以下递归生成器函数:

def adjacent_combinations(sentence, target=None, length=0):
    if not target:
        for target in set(sentence):
            for combination in adjacent_combinations(sentence, target, 1):
                yield combination
    elif length == len(sentence):
        yield [target]
    else:
        for a, b in set(zip(sentence, sentence[1:])):
            if a == target:
                for combination in adjacent_combinations(sentence, b, length + 1):
                    yield [a] + combination

这样:

list(adjacent_combinations(['a', 'B', 'c', 'a', 'D']))

会 return:

[['B', 'c', 'a', 'B', 'c'],
 ['c', 'a', 'B', 'c', 'a'],
 ['a', 'B', 'c', 'a', 'B'],
 ['a', 'B', 'c', 'a', 'D']]