I want to split a string by a list of indices, where the split segments begin with one index and end before the next one.
Example:
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
This will return:
long string that I want to split up
string that I want to split up
that I want to split up
I want to split up
I'm trying to get:
long
string
that
I want to split up
smci
30.4k18 gold badges110 silver badges145 bronze badges
asked Jun 1, 2012 at 13:43
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]]]
returns
['long ', 'string ', 'that ', 'I want to split up']
which you can print using:
print '\n'.join[parts]
Another possibility [without copying indices
] would be:
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]]
answered Jun 1, 2012 at 13:45
eumiroeumiro
198k33 gold badges294 silver badges259 bronze badges
10
Here is a short solution with heavy usage of the itertools module. The tee
function is used to iterate pairwise over the indices. See the Recipe section in the module for more help.
>>> 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']
Edit: This is a version that does not copy the indices list, so it should be faster.
flywire
91110 silver badges31 bronze badges
answered Jun 1, 2012 at 13:52
schlamarschlamar
9,0223 gold badges37 silver badges76 bronze badges
3
You can write a generator if you don't want to make any modifications to the list of indices:
>>> 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']
answered Aug 3, 2019 at 22:18
Zhou ShaoZhou Shao
1161 silver badge4 bronze badges
0
Another solution [a bit more readable]:
parts=[]; i2=len[s] #--> i1 and i2 are 'startIndex' and 'endIndex'
for i1 in reversed[indices]: parts.append[ s[i1:i2] ]; i2=i1
parts.reverse[]
This reverses the indices and therefore starts splitting from the last index position to the 'endIndex' i2 [which is updated in every loop].
Of course the elements are in the wrong order than. That's why I reversed the result array at the end.
I think for beginners this is a bit more readable than the accepted answer.
answered Jul 21 at 21:10