Re: better way to write this function

2007-11-26 Thread Peter Otten
Ricardo Aráoz wrote: > Peter Otten wrote: >> You can use slicing: >> > def chunks(items, n): >> ... return [items[start:start+n] for n in range(0, len(items)-n+1, n)] >> ... > for i in range(1,10): >> ... print chunks(range(5), i) >> ... >> [[0], [1], [2], [3], [4]] >> [[0, 1],

Re: better way to write this function

2007-11-26 Thread Ricardo Aráoz
Peter Otten wrote: > Kelie wrote: > >> Hello, >> >> This function does I what I want. But I'm wondering if there is an >> easier/better way. To be honest, I don't have a good understanding of >> what "pythonic" means yet. >> >> def divide_list(lst, n): >> """Divide a list into a number of list

Re: better way to write this function

2007-11-26 Thread Arnaud Delobelle
On Nov 26, 8:19 am, Peter Otten <[EMAIL PROTECTED]> wrote: [...] > > Or build a generator that works with arbitrary iterables: > > >>> from itertools import * > >>> def chunks(items, n): > > ... items = iter(items) > ... while 1: > ... chunk = list(islice(items, n-1)) > ...

Re: better way to write this function

2007-11-26 Thread Peter Otten
Peter Otten wrote: def chunks(items, n): > ... return [items[start:start+n] for n in range(0, len(items)-n+1, n)] Ouch, this should be def chunks(items, n): return [items[start:start+n] for start in range(0, len(items)-n+1, n)] Peter -- http://mail.python.org/mailman/listinfo/py

Re: better way to write this function

2007-11-26 Thread Paul Hankin
On Nov 26, 7:42 am, Kelie <[EMAIL PROTECTED]> wrote: > Hello, > > This function does I what I want. But I'm wondering if there is an > easier/better way. To be honest, I don't have a good understanding of > what "pythonic" means yet. > > def divide_list(lst, n): > """Divide a list into a number

Re: better way to write this function

2007-11-26 Thread Paul McGuire
On Nov 26, 1:42 am, Kelie <[EMAIL PROTECTED]> wrote: > Hello, > > This function does I what I want. But I'm wondering if there is an > easier/better way. To be honest, I don't have a good understanding of > what "pythonic" means yet. > > def divide_list(lst, n): > """Divide a list into a number

Re: better way to write this function

2007-11-26 Thread Chris Mellon
On Nov 26, 2007 3:24 AM, Paul Rudin <[EMAIL PROTECTED]> wrote: > Kelie <[EMAIL PROTECTED]> writes: > > > Hello, > > > > This function does I what I want. But I'm wondering if there is an > > easier/better way. To be honest, I don't have a good understanding of > > what "pythonic" means yet. > > > >

Re: better way to write this function

2007-11-26 Thread Kelie
On Nov 25, 11:24 pm, Paul Rudin <[EMAIL PROTECTED]> wrote: > See the last recipe from:http://docs.python.org/lib/itertools-recipes.html. > It's not doing > quite the same thing, but gives an illustration of one way to approach > this sort of thing. Thanks for the link! -- http://mail.python.or

Re: better way to write this function

2007-11-26 Thread Kelie
On Nov 25, 10:51 pm, Paul Rubin wrote: > Really though, this grouping function gets reimplemented so often that > it should be built into the stdlib, maybe in itertools. thanks Paul. itertools? that was actually the first module i checked. -- http://mail.python.org/mail

Re: better way to write this function

2007-11-26 Thread Paul Rudin
Kelie <[EMAIL PROTECTED]> writes: > Hello, > > This function does I what I want. But I'm wondering if there is an > easier/better way. To be honest, I don't have a good understanding of > what "pythonic" means yet. > > def divide_list(lst, n): > """Divide a list into a number of lists, each wi

Re: better way to write this function

2007-11-26 Thread Chris
On Nov 26, 10:51 am, Paul Rubin wrote: > Chris <[EMAIL PROTECTED]> writes: > > for i in range(int(round((len(lst)/n),0))): ... > > Ugh!!! Not even correct (under future division), besides being ugly. > I think you mean: > >for i in xrange(len(lst) // n): ... > >

Re: better way to write this function

2007-11-26 Thread Paul Rubin
Chris <[EMAIL PROTECTED]> writes: > for i in range(int(round((len(lst)/n),0))): ... Ugh!!! Not even correct (under future division), besides being ugly. I think you mean: for i in xrange(len(lst) // n): ... Really though, this grouping function gets reimplemented so often that it should

Re: better way to write this function

2007-11-26 Thread Chris
On Nov 26, 9:42 am, Kelie <[EMAIL PROTECTED]> wrote: > Hello, > > This function does I what I want. But I'm wondering if there is an > easier/better way. To be honest, I don't have a good understanding of > what "pythonic" means yet. > > def divide_list(lst, n): > """Divide a list into a number

Re: better way to write this function

2007-11-26 Thread Peter Otten
Kelie wrote: > Hello, > > This function does I what I want. But I'm wondering if there is an > easier/better way. To be honest, I don't have a good understanding of > what "pythonic" means yet. > > def divide_list(lst, n): > """Divide a list into a number of lists, each with n items. Extra >

better way to write this function

2007-11-25 Thread Kelie
Hello, This function does I what I want. But I'm wondering if there is an easier/better way. To be honest, I don't have a good understanding of what "pythonic" means yet. def divide_list(lst, n): """Divide a list into a number of lists, each with n items. Extra items are ignored, if an