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


Groups > comp.lang.python > #7033

Re: How does this work?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!news.glorb.com!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: How does this work?
References <mailman.2463.1307239834.9059.python-list@python.org>
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
Date Sun, 05 Jun 2011 13:37:52 +1000
Message-ID <878vth3q3z.fsf@benfinney.id.au> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)
Cancel-Lock sha1:NPLIvXR3bCPlKdJqyV+qirw4NvE=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 67
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 8f93887a.news.astraweb.com
X-Trace DXC=FPLG0BL\5QOm=FFS;n7SGML?0kYOcDh@JN7:H2`MmAUCQ4`Yco4[6EJ]G;2>V^?kWCbEW9A[5UK?ENZ[SL`C\KgC2GRUbEO0b:C
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7033

Show key headers only | View raw


<jyoung79@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

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


Thread

How does this work? <jyoung79@kc.rr.com> - 2011-06-04 01:51 +0000
  Re: How does this work? Ben Finney <ben+python@benfinney.id.au> - 2011-06-05 13:37 +1000
    Re: How does this work? Jon Clements <joncle@googlemail.com> - 2011-06-04 23:32 -0700
      Re: How does this work? Ben Finney <ben+python@benfinney.id.au> - 2011-06-05 16:49 +1000

csiph-web