Skip to content

Instantly share code, notes, and snippets.

@aaronsaunders
Created October 1, 2013 08:41
Show Gist options
  • Save aaronsaunders/6775537 to your computer and use it in GitHub Desktop.
Save aaronsaunders/6775537 to your computer and use it in GitHub Desktop.
Get random sample from list while maintaining ordering of items? I have a sorted list, let say: (its not really just numbers, its a list of objects that are sorted with a complicated time consuming algorithm) mylist = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , 10 ] Is there some python function that will give me N of the items, but will keep the order…
# Following code will generate a random sample of size 4.
rand_smpl = [ mylist[i] for i in sorted(random.sample(xrange(len(mylist)), 4)) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment