Skip to content Skip to sidebar Skip to footer

Split Python String By Predifned Indices

I have a string that I'd like to split in specific places into a list of strings. The split points are stored in a separate split list. For example: test_string = 'thequickbrownfox

Solution 1:

Like this?

>>> map(lambda x: test_string[slice(*x)], zip(split_points, split_points[1:]+[None]))
['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

We're ziping split_points with a shifted self, to create a list of all consecutive pairs of slice indexes, like [(0,3), (3,8), ...]. We need to add the last slice (32,None) manually, since zip terminates when the shortest sequence is exhausted.

Then we map over that list a simple lambda slicer. Note the slice(*x) which creates a slice object, e.g. slice(0, 3, None) which we can use to slice the sequence (string) with standard the item getter (__getslice__ in Python 2).

A little bit more Pythonic implementation could use a list comprehension instead of map+lambda:

>>> [test_string[i:j] for i,j in zip(split_points, split_points[1:] + [None])]
['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

Solution 2:

This may be less convoluted:

>> test_string = "thequickbrownfoxjumpsoverthelazydog">> split_points = [0, 3, 8, 13, 16, 21, 25, 28, 32]
>> split_points.append(len(test_string))
>> print([test_string[i: j] for i, j in zip(split_points, split_points[1:])])
['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

Solution 3:

First draft:

for idx, i inenumerate(split_points):
    try:
        print(test_string[i:split_points[idx+1]])
    except IndexError:
        print(test_string[i:])

Post a Comment for "Split Python String By Predifned Indices"