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


Groups > comp.lang.python > #7032

How does this work?

Date 2011-06-04 01:51 +0000
From <jyoung79@kc.rr.com>
Subject How does this work?
Newsgroups comp.lang.python
Message-ID <mailman.2463.1307239834.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext 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