I gotta have my orange juice.

Jesu, Juva

Posts Tagged ‘partition

Python list partition

leave a comment »

I came up with these simple solutions to RonJeffriesCodingProblem. The first uses Python 2.2’s generators, the second uses the functional idiom, and the third uses list comprehensions. The third is my favorite:

def split1(list, num) :
  for index in range(num) :
    yield list[index * len(list) / num:(index + 1) * len(list) / num]

def split2(list, num) :
  return map(lambda x: list[x * len(list) / num:(x+1) * len(list) / num], range(num))

def split3(list, num) :
  return [list[x*len(list)/num : (x+1)*len(list)/num] for x in range(num)]

Written by Scott Moonen

November 9, 2004 at 2:10 pm

Posted in Python

Tagged with ,