pick randomly a certain fraction of numbers from a list
Peter Hansen
peter at engcorp.com
Fri Feb 14 13:40:18 EST 2003
More information about the Python-list mailing list
Fri Feb 14 13:40:18 EST 2003
- Previous message (by thread): pick randomly a certain fraction of numbers from a list
- Next message (by thread): pick randomly a certain fraction of numbers from a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Yuval 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? >>> import random >>> a = range(50) >>> random.shuffle(a) >>> a[:int(.02*len(a))] [8] Other than deciding how you want to account for rounding errors etc, this should do it. -Peter
- Previous message (by thread): pick randomly a certain fraction of numbers from a list
- Next message (by thread): pick randomly a certain fraction of numbers from a list
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list