Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'string': 0.09; 'runtime': 0.09; 'python': 0.11; 'def': 0.12; 'random': 0.14; '(0,': 0.16; '-tkc': 0.16; '__lt__': 0.16; '__lt__(self,': 0.16; '__ne__': 0.16; 'assumptions': 0.16; 'ben,': 0.16; 'bisect': 0.16; 'calculates': 0.16; 'data)': 0.16; 'data.sort()': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'list)': 0.16; 'reedy': 0.16; 'subject:sample': 0.16; 'tuple': 0.16; 'tuple.': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'meant': 0.20; 'seems': 0.21; 'appears': 0.22; 'code,': 0.22; 'example': 0.22; 'import': 0.22; 'filtering': 0.24; "shouldn't": 0.24; 'looks': 0.24; 'source': 0.25; 'code:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'testing': 0.29; 'external': 0.29; 'thus': 0.29; 'tim': 0.29; 'compared': 0.30; 'code': 0.31; 'lines': 0.31; 'accidentally': 0.31; 'chase': 0.31; 'implicit': 0.31; 'tuples': 0.31; 'class': 0.32; 'could': 0.34; 'subject:with': 0.35; 'case,': 0.35; 'false': 0.36; 'charset:us- ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; '(i.e.,': 0.38; 'form,': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'show': 0.63; 'myself': 0.63; 'total': 0.65; 'sample': 0.67; 'default': 0.69; 'below.': 0.71; '50),': 0.84; 'data;': 0.84; 'received:50.22': 0.84 Date: Sun, 16 Feb 2014 08:22:50 -0600 From: Tim Chase To: python-list@python.org Subject: Re: random.sample with large weighted sample-sets? In-Reply-To: References: <20140215224145.68c82eb4@bigbox.christie.dr> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392560538 news.xs4all.nl 2933 [2001:888:2000:d::a6]:39724 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66538 On 2014-02-16 04:12, Terry Reedy wrote: > On 2/15/2014 11:41 PM, Tim Chase wrote: > > data = ( > > ("apple", 20), > > ("orange", 50), > > ("grape", 30), > > ) To Ben, yes, this was just some sample data; the original gets built from an external (i.e., client-supplied, thus the need to gracefully support crazy-large numbers) data source and is indeed actually a list rather than a tuple. When entering static data like this, I often default to an outer tuple (rather than list) just as a hint/reminder to myself that I don't expect this to change at runtime (and have Python yell at me if I accidentally try). > 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]) Trying to read what may be implicit assumptions in your code: 1) your code calculates "100" as sum(item[0] for item in data) 2) the data has to be sorted for bisect to work 3) you meant to write "(10, 'apple')" rather than 0. With my original example code, a 0-probability shouldn't ever show up in the sampling, where it looks like it might when using this sample code. In my particular use case, I can limit/ensure that 0-probability items never appear in the list, filtering them upon loading. 4) that "zzzzzz" is some arbitrary value that should come after any string that could appear in the data; perhaps using some custom "InfinityString" class where everything compared to it is always less than it. So it would be class InfinityString: def __gt__(self, other): True __ge__ = __gt__ def __lt__(self, other): False __eq__ = __le__ = __ne__ = __lt__ infinity_string = InfinityString() data = load_data() # list of (quantity, value) tuples data.sort() total = sum(qty for qty, value in data) for i in range(num_to_sample): r = random.randrange(0, total) i = bisect.bisect(data, (r, infinity_string)) - 1 use(data[i][1]) Some long-running testing on this code seems to show that if two items have the same probability, bisect only appears to find the last one. Tested with data = [ (10, "apple"), (20, "banana"), # I never get any bananas, even after thousands of iterations (20, "grape"), (50, "orange"), ] Thanks, -tkc