使用列表理解将字符串拆分为K个子字符串

时间:2018-11-25 10:44:30

标签: python

我和我在一起很长

s = 'abcdabcdabcdabcdabcdefghi'

我想将其拆分为K个子字符串,其中每个子字符串的长度至少应为1,即非空。我想要所有这些可能的组合。

如果K为3,我期望的输出必须类似于以下内容

[['abcda', 'bcdabcdabcda', 'bcdefghi'], [.....], [....], ... ]

我想通过列表理解来做到这一点,但是我被困住了。有可能实现吗?还有其他更快的选择吗??

2 个答案:

答案 0 :(得分:2)

使用itertools.combinations,可以获得分离索引对:

>>> s = 'abcdef'
>>> k = 3
>>> list(combinations(range(1, len(s)), k-1))
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]

使用该索引对获取字符串切片

  • (1, 2)->(s[:1]s[1:2]s[2:]
  • (1, 3)->(s[:1]s[1:3]s[3:]
  • ...
  • (4, 5)->(s[:4]s[4:5]s[5:]

>>> from itertools import combinations
>>> s = 'abcdef'

>>> k = 3
>>> [[s[i:j] for i, j in zip((None,) + idxs, idxs + (None,))]
...  for idxs in combinations(range(1, len(s)), k-1)]
[['a', 'b', 'cdef'], ['a', 'bc', 'def'], ..., ['abcd', 'e', 'f']]


>>> k = 4
>>> [[s[i:j] for i, j in zip((None,) + idxs, idxs + (None,))]
...  for idxs in combinations(range(1, len(s)), k-1)]
[['a', 'b', 'c', 'def'], ['a', 'b', 'cd', 'ef'], ..., ['abc', 'd', 'e', 'f']]
  • s[:1] == s[0:1] == s[None:1]
  • s[2:] == s[2:len(s)] == s[2:None]

答案 1 :(得分:0)

您可以找到列表中的所有切片,以便没有任何扩展库,像这样的切片部分都不会为空:

s = 'abcd'
substrings = []

# find slice of first part - from a|bcd to ab|cd
for first_slice in range(len(s)-2):
    # find slice of second and last part, for bcd - from b|cd to bc|d
    #                                     for cd -  just c|d
    for second_slice in range(first_slice+1, len(s)-1):
        substrings.append([s[:first_slice+1], s[first_slice+1: second_slice+1], s[second_slice+1:]])

print(substrings) # -> [['a', 'b', 'cd'], ['a', 'bc', 'd'], ['ab', 'c', 'd']]

s = 'abcdabcdabcdabcdabcdefghi'
print(len(substrings)) # -> 276