[SciPy-User] Picking a random element of an array with conditions

R. Padraic Springuel R.Springuel at umit.maine.edu
Fri Jun 11 14:12:27 EDT 2010


I'd like to pick the random element of an array from those elements 
which meet a certain condition.  I.e. pick an element of a for which a 
== value is True.

Without the condition, I'd phrase the command like this:
a[random.randint(len(a))]

Is there some similar thing that I can do to pick with the condition in 
an efficient manner?  So far all I've come up with involves looping over 
the array to construct an array of indecies so that I can write:
a[indecies[random.randint(len(indecies))]]

-- 

R. Padraic Springuel
Research Assistant
Department of Physics and Astronomy
University of Maine
Bennett 309
Office Hours: By Appointment Only



More information about the SciPy-User mailing list