Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66521
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: random.sample with large weighted sample-sets? |
| Date | 2014-02-16 04:12 -0500 |
| References | <20140215224145.68c82eb4@bigbox.christie.dr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7047.1392541984.18130.python-list@python.org> (permalink) |
On 2/15/2014 11:41 PM, Tim Chase wrote:
> I'm not coming up with the right keywords to find what I'm hunting.
> I'd like to randomly sample a modestly compact list with weighted
> distributions, so I might have
>
> data = (
> ("apple", 20),
> ("orange", 50),
> ("grape", 30),
> )
If you actually start with date in this form, write the few lines needed
to produce the form below.
import bisect
import random
data = [
(0, 'apple'),
(20, 'orange'),
(70, 'grape'),
]
for i in range(10):
r = random.randrange(0, 100)
i = bisect.bisect(data, (r, 'zzzzz')) - 1
print(data[i][1])
>>>
apple
orange
orange
grape
orange
apple
grape
orange
grape
orange
It is just coincidence that the sample has exactly the expected
distribution.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: random.sample with large weighted sample-sets? Terry Reedy <tjreedy@udel.edu> - 2014-02-16 04:12 -0500
csiph-web