繁体   English   中英

分割为n个字符串时,返回字符串的所有可能组合

[英]Return all possible combinations of a string when splitted into n strings

我找了一个关于这个的stackoverflow但是找不到办法去做。 它可能涉及itertools。

我想找到分割字符串的所有可能结果,比如将字符串thisisateststring写成n (等长或不等长,无关紧要,两者都应包括在内)字符串。

例如,让n3

[["thisisat", "eststrin", "g"], ["th", "isisates", "tstring"], ............]

你可以在这里使用itertools.combinations 您只需要选择两个分裂点来生成每个结果字符串:

from itertools import combinations
s = "thisisateststring"
pools = range(1, len(s))
res = [[s[:p], s[p:q], s[q:]] for p, q in combinations(pools, 2)]
print res[0]
print res[-1]

输出:

['t', 'h', 'isisateststring']
['thisisateststri', 'n', 'g']

使用itertools.combinations()在结果中包含空字符串会相当尴尬。 编写自己的递归版本可能最简单:

def partitions(s, k):
    if not k:
        yield [s]
        return
    for i in range(len(s) + 1):
        for tail in partitions(s[i:], k - 1):
            yield [s[:i]] + tail

这适用于任何数量的工作k所需分区的任何字符串s

根据Raymond Hettinger的代码,这是一个将序列分成n组的方法:

import itertools as IT

def partition_into_n(iterable, n, chain=IT.chain, map=map):
    """
    Based on http://code.activestate.com/recipes/576795/ (Raymond Hettinger)
    Modified to include empty partitions, and restricted to partitions of length n
    """
    s = iterable if hasattr(iterable, '__getslice__') else tuple(iterable)
    m = len(s)
    first, middle, last = [0], range(m + 1), [m]
    getslice = s.__getslice__
    return (map(getslice, chain(first, div), chain(div, last))
            for div in IT.combinations_with_replacement(middle, n - 1))

In [149]: list(partition_into_n(s, 3))
Out[149]: 
[['', '', 'thisisateststring'],
 ['', 't', 'hisisateststring'],
 ['', 'th', 'isisateststring'],
 ['', 'thi', 'sisateststring'],
 ...
 ['thisisateststrin', '', 'g'],
 ['thisisateststrin', 'g', ''],
 ['thisisateststring', '', '']]

它比小n的递归解决方案慢,

def partitions_recursive(s, n):
    if not n>1:
        yield [s]
        return
    for i in range(len(s) + 1):
        for tail in partitions_recursive(s[i:], n - 1):
            yield [s[:i]] + tail

s = "thisisateststring"
In [150]: %timeit list(partition_into_n(s, 3))
1000 loops, best of 3: 354 µs per loop

In [151]: %timeit list(partitions_recursive(s, 3))
10000 loops, best of 3: 180 µs per loop

但正如您所料,对于大n来说它更快(随着递归深度的增加):

In [152]: %timeit list(partition_into_n(s, 10))
1 loops, best of 3: 9.2 s per loop

In [153]: %timeit list(partitions_recursive(s, 10))
1 loops, best of 3: 10.2 s per loop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM