Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'none:': 0.05; 'badly': 0.07; 'repeated': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'satisfy': 0.09; 'second.': 0.09; 'def': 0.10; 'index': 0.13; 'resulting': 0.13; 'overwriting': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'repetitions': 0.16; 'roy': 0.16; 'set)': 0.16; 'subject:sample': 0.16; 'true:': 0.16; 'values:': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'typical': 0.17; 'yield': 0.17; 'bit': 0.21; 'assuming': 0.22; 'sets': 0.23; 'split': 0.23; 'seems': 0.23; 'header:User- Agent:1': 0.26; '(which': 0.26; 'values': 0.26; 'order.': 0.27; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'initial': 0.28; 'though.': 0.29; 'way?': 0.29; 'array': 0.29; 'expect': 0.31; 'code': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'list': 0.35; 'subject:?': 0.35; 'there': 0.35; 'list.': 0.35; 'received:org': 0.36; 'but': 0.36; '(i.e.': 0.36; 'should': 0.36; 'uses': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'between': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'population': 0.65; 'smith': 0.71; 'destructive': 0.84; 'lost,': 0.84; '100,000': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Multiple disjoint sample sets? Date: Sun, 13 Jan 2013 11:16:03 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ad4a.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358072165 news.xs4all.nl 6921 [2001:888:2000:d::a6]:45434 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36728 Roy Smith wrote: > I have a list of items. I need to generate n samples of k unique items > each. I not only want each sample set to have no repeats, but I also > want to make sure the sets are disjoint (i.e. no item repeated between > sets). > > random.sample(items, k) will satisfy the first constraint, but not the > second. Should I just do random.sample(items, k*n), and then split the > resulting big list into n pieces? Or is there some more efficient way? > > Typical values: > > len(items) = 5,000,000 > n = 10 > k = 100,000 I would expect that your simple approach is more efficient than shuffling the whole list. Assuming there is a sample_iter(population) that generates unique items from the population (which has no repetitions itself) you can create the samples with g = sample_iter(items) samples = [list(itertools.islice(g, k) for _ in xrange(n)] My ideas for such a sample_iter(): def sample_iter_mark(items): n = len(items) while True: i = int(random()*n) v = items[i] if v is not None: yield v items[i] = None This is destructive and will degrade badly as the number of None items increases. For your typical values it seems to be OK though. You can make this non-destructive by adding a bit array or a set (random.Random.sample() has code that uses a set) to keep track of the seen items. Another sample_iter() (which is also part of the random.Random.sample() implementation): def sample_iter_replace(items): n = len(items) for k in xrange(n): i = int(random()*(n-k)) yield items[i] items[i] = items[n-k-1] You can micro-optimise that a bit to avoid the index calculation. Also, instead of overwriting items you could swap them, so that no values would be lost, only their initial order.