Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 12:52 PM, Terry Reedy wrote: def group(seq, n): 'Yield from seq successive disjoint slices of length n & the remainder' if n<=0: raise ValueError('group size must be positive') for i in range(0,len(seq), n): yield seq[i:i+n] for inn,out in ( (('',1), []), # no input, no output #(('a

Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 1:29 PM, rusi wrote: On Jun 5, 11:33 pm, Terry Reedy wrote: Let me add something not said much here about designing functions: start with both a clear and succinct definition *and* test cases. (I only started writing tests first a year ago or so.) I am still one year in the futu

Re: Lambda question

2011-06-06 Thread harrismh777
Steven D'Aprano wrote: For any non-trivial function, I usually start by writing the documentation (a docstring and doctests) first. How else do you know what the function is supposed to do if you don't have it documented? Yes. In my early years I was no different than any other hacker in terms

Re: Lambda question

2011-06-06 Thread Steven D'Aprano
On Mon, 06 Jun 2011 12:52:31 -0400, Terry Reedy wrote: > Let me add something not said much here about designing functions: start > with both a clear and succinct definition *and* test cases. (I only > started writing tests first a year ago or so.) For any non-trivial function, I usually start b

Re: Lambda question

2011-06-06 Thread rusi
On Jun 5, 11:33 pm, Terry Reedy wrote: > On 6/5/2011 5:31 AM, Alain Ketterlin wrote: > > >  writes: > > > f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > > f=lambda ... statements are inferior for practical purposes to the > equivalent def f statements because the resultin

Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 9:42 AM, jyoun...@kc.rr.com wrote: f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc Packing tail recursion into one line is bad for both understanding and refactoring. Use better names and a docstring gives def group(seq, n): 'Yield from seq successive disjoin

RE: Lambda question

2011-06-06 Thread jyoung79
> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > Packing tail recursion into one line is bad for both understanding and > refactoring. Use better names and a docstring gives > > def group(seq, n): >'Yield from seq successive disjoint slices of length n plus the > re

Re: Lambda question

2011-06-05 Thread Terry Reedy
On 6/5/2011 5:31 AM, Alain Ketterlin wrote: writes: f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc f=lambda ... statements are inferior for practical purposes to the equivalent def f statements because the resulting object is missing a useful name attribute and a docstr

Re: Lambda question

2011-06-05 Thread John Posner
On 2:59 PM, Ian Kelly wrote: > On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico 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

Re: Lambda question

2011-06-05 Thread Alain Ketterlin
writes: 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-s > ized-chunks-in-python/312644 > > It doesn't work with a huge list, but l

Re: Lambda question

2011-06-04 Thread Vito 'ZeD' De Tullio
jyoun...@kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. non-recursive, same-unreadeable (worse?) one liner alternative: def chunks(s, j): return [''.join(filter(None,c))for c in map(None,*(s[i::j]for i in range(j)))] -- By ZeD -- http:/

Re: Lambda question

2011-06-04 Thread Ian Kelly
On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico 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, worl

Re: Lambda question

2011-06-04 Thread Mel
jyoun...@kc.rr.com wrote: > 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/ques

Re: Lambda question

2011-06-04 Thread Chris Angelico
On Sun, Jun 5, 2011 at 3:46 AM, wrote: > 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: >

Re: lambda question

2010-06-12 Thread Vincent Davis
On Fri, Jun 11, 2010 at 10:11 PM, Ian Kelly wrote: > On Fri, Jun 11, 2010 at 9:31 PM, Vincent Davis > wrote: >> Starting with an example. >> In [23]: x = [1,2,3,4,4,4,5,5,3,2,2,] >> In [24]: y = set(x) >> In [25]: y >> Out[25]: set([1, 2, 3, 4, 5]) >> In [26]: y2 = len(set(x)) >> In [27]: y2 >>

Re: lambda question

2010-06-11 Thread Ian Kelly
On Fri, Jun 11, 2010 at 9:31 PM, Vincent Davis wrote: > Starting with an example. > In [23]: x = [1,2,3,4,4,4,5,5,3,2,2,] > In [24]: y = set(x) > In [25]: y > Out[25]: set([1, 2, 3, 4, 5]) > In [26]: y2 = len(set(x)) > In [27]: y2 > Out[27]: 5 > > How would I do the above "y2 = len(set(x))" but ha