title: Sample with replacement in python with limit on number of samples from each class
body: <p>Python has <code>random.choices</code>, which lets you sample from a population with replacement, assigning different weights to different members of the population.  Similarly, numpy has <code>np.random.choice</code> which is similar (though it annoyingly forces you to normalize your weights into a probability distribution).  But neither function allows you to restrict how many times each member of the population can occur in the sample.  This is necessary if the members represent subpopulations and you are doing hierarchical sampling where you want to sample the members of each subpopulation without replacement since otherwise you might choose a subpopulation more times than it has members.  Is there some other library function that can do this?</p>