Skip to content

Instantly share code, notes, and snippets.

@ashish0x90
Created October 22, 2010 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashish0x90/640357 to your computer and use it in GitHub Desktop.
Save ashish0x90/640357 to your computer and use it in GitHub Desktop.
Splits a sequence and returns a generator having split subsequences - See doc string for further info.
from math import ceil
def split(input_list,num_fractions=None,subset_length=None):
'''
Given a list/tuple split original list based on either one of two parameters given but NOT both,
Returns generator
num_fractions : Number of subsets original list has to be divided into, of same size to the extent possible.
It's possible that number of fractions returned is less than requested (num_fractions value)
In case, when you want to split a 6-member list into 4 subsets.
output in that case will be 3 subsets having 2 elements each.
Think about it, makes sense.
subset_length : Split on every subset_length elements until the list is exhausted.
In case subset_length is not a proper divisor of length(input_list),
all but the last subset will have the same number of elements,
last subset will have the remaining elems - length(input_list) % subset_length
Example usage: Split a 8-element list into 4 subsets.
>> list(list_split.split(range(8),num_fractions=4))
:: [[0, 1], [2, 3], [4, 5], [6, 7]]
Split a 8-element list on every 3rd element
>>list(list_split.split(range(8),subset_length=3))
:: [[0, 1, 2], [3, 4, 5], [6, 7]]
'''
if not input_list:
yield input_list
elif not bool(num_fractions and num_fractions >=1 ) ^ \
bool(subset_length and subset_length>= 1): #validates params to be > 1
raise Exception("Only one of the params : num_fractions,subset_length (x: x>=1) to be provided")
else:
if num_fractions >= 1: #derive subset_length from num_fractions in case it's not present
subset_length = int( ceil( len(input_list) / float(num_fractions) ) )
for start in xrange(0,len(input_list),subset_length):
yield input_list[start:start+subset_length]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment