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] = [7,8,9,10]

obviously not even, one list will have 4 elements, the other 2 will
have 3.,
the overriding logic, is that I will get 3 lists and find a way for
python to try to break it evenly, if not one list can have a greater
amount of elements

Would I use itertools ? How would I do this ?

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


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 would like to maintain the original order of the list.
-- 
http://mail.python.org/mailman/listinfo/python-list