按索引列表拆分字符串

时间:2012-06-01 13:43:23

标签: python split

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

示例:

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

这将返回:

  

我要拆分的长串   我要拆分的字符串
  我要分手了   我想分手

我想要:

  


  串
  该
  我想分手

3 个答案:

答案 0 :(得分:22)

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)]

答案 1 :(得分:3)

以下是大量使用itertools module的简短解决方案。 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']

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

答案 2 :(得分:0)

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

>>> 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']