map, filter, reduce, zip, range, and.. slice?
Eddie Corns
eddie at holyrood.ed.ac.uk
Thu Nov 1 10:19:11 EST 2001
Eyal Lotem <eyal at hyperroll.com> writes:
>Hey. Recently, I've been heavily using functional-style programming in
>Python, doing almost all of my text processing and so with complex nested
>lambda clauses. I love those, as they are so short, and seem to work with
>a painless "debug" cycle, usually consisting of some missing reduce initial
>value or so.
>To get to the point, I've almost always lacked a 'slice' function, that
>slices sequences into smaller chunks. For example:
>divide("Hello world!", 2) => ["He", "ll", "o ", "wo", "rl", "d!"]
It's a bit clumsy but how about:
[x[i:i+2] for i in range(0,len(x),2)]
Where x is your string.
Eddie
More information about the Python-list
mailing list