Simple List division problem

Fredrik Lundh fredrik at pythonware.com
Sun Jan 13 08:02:13 EST 2008


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))
>     return z
> 
> gives 

>>>> chop([1], 3)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "beforemeth.py", line 9, in chop
>     if len(z[-1]) < n and len(z) > 1:
> ValueError: xrange() arg 3 must not be zero

well, it doesn't.  there's no xrange on that line.

 > Perhaps something like this?

 > from itertools import islice

or just use an if-statement, or the max function.  but I guess those 
tools are too old and boring for c.l.python these days...

</F>




More information about the Python-list mailing list