pick randomly a certain fraction of numbers from a list
Jp Calderone
exarkun at intarweb.us
Fri Feb 14 15:11:27 EST 2003
On Fri, Feb 14, 2003 at 12:59:41PM -0600, Skip Montanaro wrote:
> >> Does anybody know a quick way of picking randomly x% of elements from
> >> a list. e.g. If I have a list of 50 elements, how do I randomly
> >> choose 2% of it? The trivial way would be to iterate with the
> >> random.choice() function. Is there any better way?
>
> How about
>
> random.shuffle(mylist)
> picks = mylist[:int(len(mylist)*0.02)]
>
> ? Actually, since 2% of 50 is 1, this would work for your particular case:
>
> random.shuffle(mylist)
> picks = mylist[:1]
As does
picks = [random.choice(mylist)]
<2%-wink>
Jp
--
up 6 days, 0:29, 5 users, load average: 0.00, 0.00, 0.00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030214/ced74c29/attachment.sig>
More information about the Python-list
mailing list