Use random.sample: >>> from random import sample >>> items = range(100) >>> sample(items, 3) [5, 61, 70] >>> sample(items, 3) [49, 73, 69] >>> sample(items, 3) [64, 77, 30] >>> sample(items, 3) [3, 54, 59] >>> sample(items, 3) [55, 36, 31]
- Ofer www.mrbroken.com On Mon, Mar 21, 2011 at 1:41 AM, Viktoras <[email protected]> wrote: > if you're going for probability based percentage, then just do that: > > import random > > def randomPercentage(data,percentage): > > for i in data: > > if random.random()<percentage: > > yield i > > > print list(randomPercentage(selectedList,0.7)) > > > also, another very short answer would be just randomizing your list and > getting a part of it: > > import random > import math > random.shuffle(selectedList) > print selectedList[:int(math.ceil(len(a)*0.7))] > > > > On 2011.03.20 22:51, Ling wrote: > >> Hey guys: >> >> Need some algorithm help if possible.. >> >> Say I have 1000 points in a sorted list, and I want to select a >> given portion of the points in the list, for example, 70% of it. Don't >> need to be perfectly accurate, but the 70% have to be pretty much >> evenly distributed among the 1000 points. >> >> I am doing a step selection right now, which is something like: >> >> >> >> selectedList = [] >> remain = 0 >> while i< 1000: >> step = int(1/0.7 + remain) >> remain = 1/0.7 + remain - step >> selectedList.append(pointsList[i]) >> i += step >> >> >> the step selection works, but just wonder if there is any better/ >> faster solution I can borrow from you guys. >> >> Any suggestions would be really appreciated. >> >> -ling >> >> > > -- > Viktoras > www.neglostyti.com > > > -- > http://groups.google.com/group/python_inside_maya > -- http://groups.google.com/group/python_inside_maya
