Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How does this work?

20 views
Skip to first unread message

jyou...@kc.rr.com

unread,
Jun 3, 2011, 9:51:55 PM6/3/11
to pytho...@python.org
I was surfing around looking for a way to split a list into equal sections. I came
upon this algorithm:

>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc
>>> f("Hallo Welt", 3)
['Hal', 'lo ', 'Wel', 't']

(http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312644)

It doesn't work with a huge list, but looks like it could be handy in certain
circumstances. I'm trying to understand this code, but am totally lost. I
know a little bit about lambda, as well as the ternary operator, but how
does this part work:

>>> f('dude'[3:], 3, []+[('dude'[:3])])
['dud', 'e']

Is that some sort of function call, or something else? I'm guessing it works
recursively?

Just curious if anyone could explain how this works or maybe share a link
to a website that might explain this?

Thanks.

Jay

Ben Finney

unread,
Jun 4, 2011, 11:37:52 PM6/4/11
to
<jyou...@kc.rr.com> writes:

> I was surfing around looking for a way to split a list into equal
> sections. I came upon this algorithm:
>
> >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc
> >>> f("Hallo Welt", 3)
> ['Hal', 'lo ', 'Wel', 't']
>
> (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312644)

This is an excellent example of why “clever” code is to be shunned.
Whoever wrote this needs to spend more time trying to get their code
past a peer review; the above would be rejected until it was re-written
to be clear.

Here is my attempt to write the above to be clear (and fixing a couple
of bugs too):

def split_slices(seq, slicesize, accumulator=None):
""" Return a list of slices from `seq` each of size `slicesize`.

:param seq: The sequence to split.
:param slicesize: The maximum size of each slice.
:param accumulator: A sequence of existing slices to which
ours should be appended.
:return: A list of the slices. Each item will be a slice
from the original `seq` of `slicesize` length; the last
item may be shorter if there were fewer than `slicesize`
items remaining.

"""
if accumulator is None:
accumulator = []
if seq:
slice = seq[:slicesize]
result = split_slices(
seq[slicesize:], slicesize, accumulator + [slice])
else:
result = accumulator
return result



> It doesn't work with a huge list, but looks like it could be handy in
> certain circumstances. I'm trying to understand this code, but am
> totally lost. I know a little bit about lambda, as well as the ternary
> operator

In Python, ‘lambda’ is merely an alternative syntax for creating
function objects. The resulting object *is* a function, so I've written
the above using the ‘def’ syntax for clarity.

The ternary operator is often useful for very simple expressions, but
quickly becomes too costly to read when the expression is complex. The
above is one where the writer is so much in love with the ternary
operator that they have crammed far too much complexity into a single
expression.

> Just curious if anyone could explain how this works or maybe share a link
> to a website that might explain this?

Does the above help?

--
\ “We must find our way to a time when faith, without evidence, |
`\ disgraces anyone who would claim it.” —Sam Harris, _The End of |
_o__) Faith_, 2004 |
Ben Finney

Jon Clements

unread,
Jun 5, 2011, 2:32:16 AM6/5/11
to
On Jun 5, 4:37 am, Ben Finney <ben+pyt...@benfinney.id.au> wrote:

> <jyoun...@kc.rr.com> writes:
> > I was surfing around looking for a way to split a list into equal
> > sections. I came upon this algorithm:
>
> > >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc
> > >>> f("Hallo Welt", 3)
> > ['Hal', 'lo ', 'Wel', 't']
>
> > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...)

Just my 2p, but isn't the itertools "grouper" recipe prudent?

Ben Finney

unread,
Jun 5, 2011, 2:49:51 AM6/5/11
to
Jon Clements <jon...@googlemail.com> writes:

> On Jun 5, 4:37 am, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> > <jyoun...@kc.rr.com> writes:
> > > (http://stackoverflow.com/questions/312443/how-do-you-split-a-list-int...)
> >
> > This is an excellent example of why “clever” code is to be shunned.
> > Whoever wrote this needs to spend more time trying to get their code
> > past a peer review; the above would be rejected until it was
> > re-written to be clear.
> >
> > Here is my attempt to write the above to be clear (and fixing a couple
> > of bugs too):

> Just my 2p, but isn't the itertools "grouper" recipe prudent?

Oh, if we go looking for ways to improve what that code is doing, there
are many things wrong with it, not least that it is re-implementing code
that already exists in the standard library.

But I'll leave that to the several superior answers at the Stackoverflow
question.

--
\ “Pray, v. To ask that the laws of the universe be annulled in |
`\ behalf of a single petitioner confessedly unworthy.” —Ambrose |
_o__) Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney

0 new messages