Newbie question about Re

Fredrik Lundh fredrik at pythonware.com
Tue Aug 24 10:43:59 EDT 1999


MB <ritm at gnet.tn> wrote:
> I just want to split a string of n x (p characters) into a list of
> p_length strings using the Re module.

umm.  since you don't really care what's IN the string,
how about using string slicing instead?  it's not only
more pythonish, it's also faster (at least in this case):

def mysplit(s, p):
    out = []
    for i in range(0, len(s), p):
        out.append(s[i:i+p])
    return out

or, if you prefer one-liners:

    list = map(lambda i, s=s, p=p: s[i:i+p], range(0, len(s), p))

</F>

    "Some people, when confronted with a problem,
    think 'I know, I'll use regular expressions.'  Now
    they have two problems." -- jwz





More information about the Python-list mailing list