itertools.product确实应该能够为您提供帮助。
这个想法是这样的:-考虑A1,A2,…,AN由平板分开。将有N-1个平板。如果有平板,则存在分段。如果没有平板,则存在联接。因此,对于给定的长度为N的序列,您应具有2
^(N-1)个这样的组合。
就像下面
import itertoolslst = ['a', 'b', 'c', 'd']combinatorics = itertools.product([True, False], repeat=len(lst) - 1)solution = []for combination in combinatorics: i = 0 one_such_combination = [lst[i]] for slab in combination: i += 1 if not slab: # there is a join one_such_combination[-1] += lst[i] else: one_such_combination += [lst[i]] solution.append(one_such_combination)print solution
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)