Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to randomly partition a list into n nearly equal parts?

Tags:

I have read the answers to the Slicing a list into n nearly-equal-length partitions [duplicate] question.

This is the accepted answer:

def partition(lst, n):      division = len(lst) / float(n)      return [ lst[int(round(division * i)): int(round(division * (i + 1)))] for i in xrange(n) ] 

I am wondering, how does one modify these solutions in order to randomly assign items to a partition as opposed to incremental assignment.

like image 316
Darren J. Fitzpatrick Avatar asked Jul 28 '10 12:07

Darren J. Fitzpatrick


1 Answers

Call random.shuffle() on the list before partitioning it.

like image 181
bobince Avatar answered Oct 07 '22 23:10

bobince