Re: Simple List division problem

2008-01-14 Thread Pierre Quentel
On 12 jan, 19:37, marcstuart <[EMAIL PROTECTED]> wrote: > How do I divide a list into a set group of sublist's- if the list is > not evenly dividable ? > consider this example: > > x = [1,2,3,4,5,6,7,8,9,10] > y = 3 # number of lists I want to break x into > z = y/x > > what I would like to ge

Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 13, 2:02 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > thebjorn wrote: > > > Eh... > > oh, forgot that it was "pulling requirements out of thin air" week on > c.l.python. Well, the OP requirements were to control the number of chunks, not the size of them, so I guess we both got it wrong

Re: Simple List division problem

2008-01-13 Thread Paul Rubin
thebjorn <[EMAIL PROTECTED]> writes: > Perhaps something like this? > > def chop(lst, length): > from itertools import islice > it = iter(lst) > z = [list(islice(it, length)) for i in xrange(1 + len(lst) // length)] > if len(z) > 1: > z[-2].extend(z.pop()) # the last item w

Re: Simple List division problem

2008-01-13 Thread Fredrik Lundh
thebjorn wrote: > Eh... oh, forgot that it was "pulling requirements out of thin air" week on c.l.python. > def chop(lst, length): > n = len(lst) / length > z = [lst[i:i+n] for i in xrange(0, len(lst), n)] > if len(z[-1]) < n and len(z) > 1: > z[-2].extend(z.pop(-1)) >

Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 13, 1:05 pm, thebjorn <[EMAIL PROTECTED]> wrote: > On Jan 12, 8:33 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > > > > > marcstuart wrote: > > > How do I divide a list into a set group of sublist's- if the list is > > > not evenly dividable ? consider this example: > > > > x = [1,2,3,4,5,6

Re: Simple List division problem

2008-01-13 Thread thebjorn
On Jan 12, 8:33 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > marcstuart wrote: > > How do I divide a list into a set group of sublist's- if the list is > > not evenly dividable ? consider this example: > > > x = [1,2,3,4,5,6,7,8,9,10] > > y = 3 # number of lists I want to break x into > > z

Re: Simple List division problem

2008-01-12 Thread Dustan
On Jan 12, 2:25 pm, Paul Rubin wrote: > marcstuart <[EMAIL PROTECTED]> writes: > > what I would like to get is 3 sublists > > > print z[0] = [1,2,3] > > print z[2] = [4,5,6] > > print z[3] = [7,8,9,10] > > Are you SURE you want that? In almost every situation I've seen,

Re: Simple List division problem

2008-01-12 Thread Paul Rubin
marcstuart <[EMAIL PROTECTED]> writes: > what I would like to get is 3 sublists > > print z[0] = [1,2,3] > print z[2] = [4,5,6] > print z[3] = [7,8,9,10] Are you SURE you want that? In almost every situation I've seen, print z[0] = [1,2,3] print z[2] = [4,5,6] print z[3] = [7,8,9

Re: Simple List division problem

2008-01-12 Thread [EMAIL PROTECTED]
On Jan 12, 12:37 pm, marcstuart <[EMAIL PROTECTED]> wrote: > How do I divide a list into a set group of sublist's- if the list is > not evenly dividable ? > consider this example: > > x = [1,2,3,4,5,6,7,8,9,10] > y = 3      # number of lists I want to break x into > z = y/x > > what I would like to

Re: Simple List division problem

2008-01-12 Thread Fredrik Lundh
marcstuart wrote: > How do I divide a list into a set group of sublist's- if the list is > not evenly dividable ? consider this example: > > x = [1,2,3,4,5,6,7,8,9,10] > y = 3 # number of lists I want to break x into > z = y/x > > what I would like to get is 3 sublists > > print z[0] = [1

Re: Simple List division problem

2008-01-12 Thread Gary Herron
marcstuart wrote: > How do I divide a list into a set group of sublist's- if the list is > not evenly dividable ? > consider this example: > > x = [1,2,3,4,5,6,7,8,9,10] > y = 3 # number of lists I want to break x into > z = y/x > > > what I would like to get is 3 sublists > > print z[0] = [1,

Re: Simple List division problem

2008-01-12 Thread marcstuart
I have gotten a little further, but not in the order of the original list def divide_list(lst, n): return [lst[i::n] for i in range(n)] x = [1,2,3,4,5,6,7,8,9,10] y = 3 z = divide_list(x,y) print z[0] print z[1] print z[2] this prints: [1, 4, 7, 10] [2, 5, 8] [3, 6, 9] closer, but I w

Simple List division problem

2008-01-12 Thread marcstuart
How do I divide a list into a set group of sublist's- if the list is not evenly dividable ? consider this example: x = [1,2,3,4,5,6,7,8,9,10] y = 3 # number of lists I want to break x into z = y/x what I would like to get is 3 sublists print z[0] = [1,2,3] print z[2] = [4,5,6] print z[3] =