Cutting a string in equal pieces

Gribouillis 1 Tallied Votes 612 Views Share

This snippet shows a fast and efficient way to cut strings in substrings with a fixed length (note that the last substring may be smaller).

import re
split5 = re.compile(r".{1,5}", re.DOTALL).findall

if __name__ == "__main__":
    print split5('"Give me bacon and eggs," said the other man.')
    # prints ['"Give', ' me b', 'acon ', 'and e', 'ggs,"', ' said', ' the ', 'other', ' man.']
Gribouillis 1,391 Programming Explorer Team Colleague

An alternative is this

splitIt =  lambda d, s: [s[i:i+d] for i in range(0, len(s), d)]

if __name__ == "__main__":
    print splitIt(5, '"Give me bacon and eggs," said the other man.')
Gribouillis 1,391 Programming Explorer Team Colleague

The following benchmark shows that, on my machine, split5() is about 22% faster than splitIt()

# benchmark for split5 and splitIt (tested with python 2.6)
from timeit import Timer
import re
split5 = re.compile(r".{1,5}", re.DOTALL).findall
splitIt =  lambda d, s: [s[i:i+d] for i in range(0, len(s), d)]

if __name__=='__main__':

    t = Timer("split5('Give me bacon and eggs, said the other man.')", "from __main__ import split5")
    print "split5: %.2f microsec"  % t.timeit()
    t = Timer("splitIt(5, 'Give me bacon and eggs, said the other man.')", "from __main__ import splitIt")
    print "splitIt: %.2f microsec"  % t.timeit()

""" my output --->
split5: 2.55 microsec
splitIt: 3.27 microsec
"""

however, you can gain 0.2 microsecs by replacing range with xrange in splitIt()

lrh9 95 Posting Whiz in Training

I wonder how list slicing or struct.unpack() would work.

bumsfeld 413 Nearly a Posting Virtuoso

Seems to me that you also have to consider the time that re.compile() needs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.