A better way to split up a list

F

fidtz

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)
 
T

Tim Chase

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)
 
J

John Machin

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)
 
F

fidtz

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.
 
L

Larry Bates

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
 
L

Larry Bates

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
 
T

Tim Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top