Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7017

Re: Lambda question

References <20110604174624.V8FO6.213264.root@cdptpa-web07-z02> <BANLkTi==zFAgdPHk0TjyMESRcL=F1ZhpkA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-06-04 13:28 -0600
Subject Re: Lambda question
Newsgroups comp.lang.python
Message-ID <mailman.2456.1307215764.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico <rosuav@gmail.com> wrote:
> Python doesn't seem to have an inbuilt function to divide strings in
> this way. At least, I can't find it (except the special case where n
> is 1, which is simply 'list(string)'). Pike allows you to use the
> division operator: "Hello, world!"/3 is an array of 3-character
> strings. If there's anything in Python to do the same, I'm sure
> someone else will point it out.

Not strictly built-in, but using the "grouper" recipe from the
itertools docs, one could do this:

def strsection(x, n):
    return map(''.join, grouper(n, x, ''))

Cheers,
Ian

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Lambda question Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-04 13:28 -0600

csiph-web