471,055 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,055 software developers and data experts.

A better way to split up a list

The code below works fine, but it is less than nice to look at and
somewhat long winded. Is there a better way to do the docstring task?
This is the first working version, and can proabably be "compacted" a
bit (list comprehensions etc) but I am looking for a better basic
approach. Any help much appreciated :)

from itertools import islice
def chopupmoves(movelist):
'''creates a list of 3 lists from one list, that should all have
(length of movelist)/3 length, with any remainder getting added to the
third list'''
outputlist = [[],[],[]]

parlen = int(len(movelist)/3)
if parlen < 3:
parlen = 3

stoplist=[0];exit = 0
while exit < len(movelist):
stoplist.append(exit+parlen)
exit = exit + parlen
while len(stoplist) > 4:
stoplist.pop(len(stoplist)-1)
stoplist[-1]=len(movelist)

for x in range(len(stoplist)-1):
for i in islice(movelist,stoplist[x],stoplist[x+1],1):
outputlist[x].append(i)
return outputlist
movelist = [1,2,3,4,5,6,7,8,9,10,11]
print chopupmoves(movelist)

May 8 '06 #1
7 1262
> The code below works fine, but it is less than nice to
look at and somewhat long winded. Is there a better way
to do the docstring task? This is the first working
version, and can proabably be "compacted" a bit (list
comprehensions etc) but I am looking for a better basic
approach. Any help much appreciated :)


It looks like there may be some problems with your code,
such that it doesn't "work fine"...particularly in lists
with 1, 2, or 3 elements.

The below "chopupmoves2" function does what you describe,
and is nice and short. A bunch of test-cases get generated
and compared so you can see their output. The
chopupmoves2() function regularly returns your described
results, while things seem a bit off with the chopupmoves()

HTH,

-tkc

from itertools import islice
def chopupmoves2(movelist):
size = len(movelist) / 3
return [
movelist[:size],
movelist[size:size*2],
movelist[size*2:]]

def chopupmoves(movelist):
'''creates a list of 3 lists from one list, that should
all have
(length of movelist)/3 length, with any remainder getting
added to the
third list'''
outputlist = [[],[],[]]

parlen = int(len(movelist)/3)
if parlen < 3:
parlen = 3

stoplist=[0];exit = 0
while exit < len(movelist):
stoplist.append(exit+parlen)
exit = exit + parlen
while len(stoplist) > 4:
stoplist.pop(len(stoplist)-1)
stoplist[-1]=len(movelist)

for x in range(len(stoplist)-1):
for i in islice(movelist,stoplist[x],stoplist[x+1],1):
outputlist[x].append(i)
return outputlist
# test them with a bunch of test-cases
movelist =[]
for i in xrange(1,12):
movelist.append(i)
print "Movelist:", movelist
print "\toriginal:", chopupmoves(movelist)
print "\tTim's: ", chopupmoves2(movelist)
May 8 '06 #2
On 8/05/2006 10:45 PM, fi***@clara.co.uk wrote:
The code below works fine, but it is less than nice to look at and
somewhat long winded. Is there a better way to do the docstring task?
This is the first working version, and can proabably be "compacted" a
bit (list comprehensions etc) but I am looking for a better basic
approach. Any help much appreciated :)

from itertools import islice
def chopupmoves(movelist):
'''creates a list of 3 lists from one list, that should all have
(length of movelist)/3 length, with any remainder getting added to the
third list'''
outputlist = [[],[],[]]

parlen = int(len(movelist)/3)
if parlen < 3:
parlen = 3 What's this for? It causes weird things to happen with short lists.

stoplist=[0];exit = 0
while exit < len(movelist):
stoplist.append(exit+parlen)
exit = exit + parlen
while len(stoplist) > 4:
stoplist.pop(len(stoplist)-1)
stoplist[-1]=len(movelist)

for x in range(len(stoplist)-1):
for i in islice(movelist,stoplist[x],stoplist[x+1],1):
outputlist[x].append(i)
return outputlist
movelist = [1,2,3,4,5,6,7,8,9,10,11]
print chopupmoves(movelist)


Unless I've totally misunderstood your spec, you don't need all that
islice and stoplist stuff.

def chopupmoves2(movelist):
parlen = len(movelist) // 3
return [movelist[:parlen], movelist[parlen:parlen*2],
movelist[parlen*2:]]

for k in range(13):
mlist = range(k)
print
print "v1", chopupmoves(mlist)
print "v2", chopupmoves2(mlist)

May 8 '06 #3
Argh, embarassment on my part due to incomplete spec. movelist is used
to store a list of chess moves and splitting it up in this way is so it
can be printed in a space that is 3 lines high but quite wide. Thus the
way the list grows as it is appended to in chopupmoves is intended.

May 8 '06 #4
fi***@clara.co.uk wrote:
The code below works fine, but it is less than nice to look at and
somewhat long winded. Is there a better way to do the docstring task?
This is the first working version, and can proabably be "compacted" a
bit (list comprehensions etc) but I am looking for a better basic
approach. Any help much appreciated :)

from itertools import islice
def chopupmoves(movelist):
'''creates a list of 3 lists from one list, that should all have
(length of movelist)/3 length, with any remainder getting added to the
third list'''
outputlist = [[],[],[]]

parlen = int(len(movelist)/3)
if parlen < 3:
parlen = 3

stoplist=[0];exit = 0
while exit < len(movelist):
stoplist.append(exit+parlen)
exit = exit + parlen
while len(stoplist) > 4:
stoplist.pop(len(stoplist)-1)
stoplist[-1]=len(movelist)

for x in range(len(stoplist)-1):
for i in islice(movelist,stoplist[x],stoplist[x+1],1):
outputlist[x].append(i)
return outputlist
movelist = [1,2,3,4,5,6,7,8,9,10,11]
print chopupmoves(movelist)


Try something like this:

def chopupmoves(movelist):
return [movelist[0:3], movelist[3:6], movelist[6:]]
-Larry Bates
May 8 '06 #5
fi***@clara.co.uk wrote:
The code below works fine, but it is less than nice to look at and
somewhat long winded. Is there a better way to do the docstring task?
This is the first working version, and can proabably be "compacted" a
bit (list comprehensions etc) but I am looking for a better basic
approach. Any help much appreciated :)

from itertools import islice
def chopupmoves(movelist):
'''creates a list of 3 lists from one list, that should all have
(length of movelist)/3 length, with any remainder getting added to the
third list'''
outputlist = [[],[],[]]

parlen = int(len(movelist)/3)
if parlen < 3:
parlen = 3

stoplist=[0];exit = 0
while exit < len(movelist):
stoplist.append(exit+parlen)
exit = exit + parlen
while len(stoplist) > 4:
stoplist.pop(len(stoplist)-1)
stoplist[-1]=len(movelist)

for x in range(len(stoplist)-1):
for i in islice(movelist,stoplist[x],stoplist[x+1],1):
outputlist[x].append(i)
return outputlist
movelist = [1,2,3,4,5,6,7,8,9,10,11]
print chopupmoves(movelist)


Try something like this:

def chopupmoves(movelist):
return [movelist[0:3], movelist[3:6], movelist[6:]]
-Larry Bates
May 8 '06 #6
> Argh, embarassment on my part due to incomplete spec.
movelist is used to store a list of chess moves and
splitting it up in this way is so it can be printed in a
space that is 3 lines high but quite wide. Thus the way
the list grows as it is appended to in chopupmoves is
intended.


Perhaps the following was what you mean?

def chop(movelist, columns=3):
result = []
itemsInColumn = (columns+len(movelist)-1)/columns
for i in range(columns):
result.append(movelist[
i*itemsInColumn:
(i+1)*itemsInColumn])
return result
# exercise the function:
for i in xrange(30):
moves = range(1,i+1)
print chop(moves)
print chop(moves, 4)

This is a fairly generic columnization routine that will
track overflow nicely.

-tkc


May 8 '06 #7
Thanks very much, that is a far tidier solution!!

May 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
2 posts views Thread by Richard Shea | last post: by
5 posts views Thread by oliver | last post: by
9 posts views Thread by Will McGugan | last post: by
3 posts views Thread by John Salerno | last post: by
3 posts views Thread by David Pratt | last post: by
24 posts views Thread by markscala | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.