36

我想通过索引列表拆分字符串,其中拆分段以一个索引开始并在下一个索引之前结束。

例子:

s = 'long string that I want to split up'
indices = [0,5,12,17]
parts = [s[index:] for index in indices]
for part in parts:
    print part

这将返回:

我要拆分的长字符串 我要拆分的字符串 我 要拆分的字符串 我 要拆分的
字符串

我试图得到:

我要拆分 的长
字符串

4

3 回答 3

50
s = 'long string that I want to split up'
indices = [0,5,12,17]
parts = [s[i:j] for i,j in zip(indices, indices[1:]+[None])]

返回

['long ', 'string ', 'that ', 'I want to split up']

您可以使用以下方式打印:

print '\n'.join(parts)

另一种可能性(不复制indices)是:

s = 'long string that I want to split up'
indices = [0,5,12,17]
indices.append(None)
parts = [s[indices[i]:indices[i+1]] for i in xrange(len(indices)-1)]
于 2012-06-01T13:45:36.970 回答
4

这是一个大量使用itertools 模块的简短解决方案。该tee函数用于对索引进行成对迭代。有关更多帮助,请参阅模块中的“配方”部分。

>>> from itertools import tee, izip_longest
>>> s = 'long string that I want to split up'
>>> indices = [0,5,12,17]
>>> start, end = tee(indices)
>>> next(end)
0
>>> [s[i:j] for i,j in izip_longest(start, end)]
['long ', 'string ', 'that ', 'I want to split up']

编辑:这是一个不复制索引列表的版本,所以它应该更快。

于 2012-06-01T13:52:37.160 回答
3

如果您不想对索引列表进行任何修改,您可以编写一个生成器:

>>> def split_by_idx(S, list_of_indices):
...     left, right = 0, list_of_indices[0]
...     yield S[left:right]
...     left = right
...     for right in list_of_indices[1:]:
...         yield S[left:right]
...         left = right
...     yield S[left:]
... 
>>> 
>>> 
>>> s = 'long string that I want to split up'
>>> indices = [5,12,17]
>>> [i for i in split_by_idx(s, indices)]
['long ', 'string ', 'that ', 'I want to split up']
于 2019-08-03T22:18:24.250 回答