mercredi 28 janvier 2015

Fast, independent random draws / sample from a list in python - fixed probability, not total number

I'd like to draw a sample of items from a list, but I want to set the probability each item is included, not the total number of items to draw (so random.sample() does not work). I get the effect I want with the following code (where p is probability of inclusion, and items is the list of things):



[item for item in items if random.random() < p]


But it is very slow. Any suggestions for speeding it up?


Thanks!


Nick





Aucun commentaire:

Enregistrer un commentaire