Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当拆分为n个字符串时,返回字符串的所有可能组合_Python_String_List_Chunks - Fatal编程技术网

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

Python 当拆分为n个字符串时,返回字符串的所有可能组合,python,string,list,chunks,Python,String,List,Chunks,我为此搜索了stackoverflow,但找不到方法。它可能涉及itertools 我想找到将字符串拆分的所有可能结果,比如字符串thisisateststring拆分为n(长度相等或不相等,都应该包括在内)字符串 例如,让n成为3: [["thisisat", "eststrin", "g"], ["th", "isisates", "tstring"], ............] 您可以在此处使用itertools.compositions。只需选择两个拆分点即可生成每个结果字符串: f

我为此搜索了stackoverflow,但找不到方法。它可能涉及itertools

我想找到将字符串拆分的所有可能结果,比如字符串
thisisateststring
拆分为
n
(长度相等或不相等,都应该包括在内)字符串

例如,让
n
成为
3

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

您可以在此处使用
itertools.compositions
。只需选择两个拆分点即可生成每个结果字符串:

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.combines()
,在结果中包含空字符串将非常麻烦。编写自己的递归版本可能是最简单的:

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

这将适用于任何字符串
s
的任意数量
k
的所需分区。以下是将序列划分为n组的方法,基于:



它比小型
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
(随着递归深度的增加),它会更快:


允许使用空子字符串吗?从数学上讲,你是在寻找长度(字符串)的所有不同大小的可能和,然后将它们排列?@C.B:从他的问题中,我了解到他只想要所有可能的方法将字符串拆分为n个子字符串(所以当你把s1+s2+s3组合起来时,你会得到原始字符串)。斯文马纳奇,是的,它们是允许的。C.B.,是的,你可以这样说,但保罗的话听起来更简洁,但可能和你说的一样。保罗,完全正确。这不包括空字符串,空字符串的程度很难理解。我知道这是一个愚蠢的问题(非常简单)问题,但有没有方法让它返回一个列表,因为当我执行它返回的代码时:像调用
list(partitions(“abcde”,3))
一样调用它。
In [149]: list(partition_into_n(s, 3))
Out[149]: 
[['', '', 'thisisateststring'],
 ['', 't', 'hisisateststring'],
 ['', 'th', 'isisateststring'],
 ['', 'thi', 'sisateststring'],
 ...
 ['thisisateststrin', '', 'g'],
 ['thisisateststrin', 'g', ''],
 ['thisisateststring', '', '']]
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
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