Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.03; 'elif': 0.04; '"c"': 0.07; '[],': 0.07; 'item.': 0.07; 'list?': 0.07; '"a"': 0.09; 'empty,': 0.09; 'item,': 0.09; 'length.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'aug': 0.13; '"b"': 0.16; '"d"': 0.16; 'alist': 0.16; 'comments:': 0.16; 'did,': 0.16; 'duplicates': 0.16; 'iterated': 0.16; 'list)': 0.16; 'long...': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sublist': 0.16; 'items.': 0.17; 'handles': 0.18; 'appears': 0.18; 'all,': 0.21; 'work,': 0.22; 'random': 0.24; 'least': 0.25; 'appear': 0.26; '???': 0.27; 'header:X-Complaints-To:1': 0.28; 'probability': 0.29; 'source': 0.29; "i'm": 0.29; 'lists': 0.31; 'gets': 0.32; 'could': 0.32; 'goes': 0.33; 'url:home': 0.33; 'problem': 0.33; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'requirements': 0.33; 'list': 0.35; 'needed': 0.35; 'whatever': 0.35; 'lists.': 0.35; 'doing': 0.35; 'continue': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'test': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'item': 0.37; 'previous': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'growing': 0.38; 'some': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'remove': 0.61; 'provide': 0.62; 'different': 0.63; 'within': 0.64; 'choose': 0.65; 'overall': 0.66; 'fact,': 0.69; 'goal': 0.74; '19%': 0.84; 'running,': 0.84; 'dennis': 0.91; 'lists:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Probability Algorithm Date: Sat, 25 Aug 2012 16:47:25 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-19-241.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 107 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345927660 news.xs4all.nl 6929 [2001:888:2000:d::a6]:37185 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27886 On Sun, 26 Aug 2012 00:03:07 +0800, ??? declaimed the following in gmane.comp.python.general: > Hi, All, > > I have a problem of probability algorithm > Smells like homework -- but since you did provide something that could be turned in as your work, I'm going to go out on a limb > > The goal is obtain a list which contains three items. as the *FinalList* > > There has Four source lists. * > ALIST, BLIST, CLIST, DLIST > > There are all Unknown length. They contains unique elements* > ( In fact, there are all empty at the program beginning, when running, > there growing ) > theLists = { "A" : [], "B" : [], "C" : [], "D" : [] } # do whatever is needed to populate the lists # I presume reading some file(s) and doing ... theLists[listName].append(listValue) > Choose items form this source lists. pick up random items to generate the > FinalList > Ensure The Following Requirements > > In the FinalList, > probability of ALIST's item appeared is 43% > probability of BLIST's item appeared is 37% > probability of CLIST's item appeared is 19% > probability of DLIST's item appeared is 1% > > theProbabilities = { "A" : 43, "B" : 37+43, "C" : 19+37+43, "D" : 1+19+37+43 } > while a_picked_times < 43: Why keep a counter? Rather than an iterated loop for i in range(43): > item = choice(ALIST) > ALIST.remove(item) Do you really want to remove an item from the source list? Technically, a requirement that an item appears from ALIST 43% of the time does NOT prohibit it being the SAME ITEM. {This also answers you problem about only working if the lists are long... as long as the list contains at least ONE item, you can pick that item to meet the probability} > > if item in already_picked_list: > continue > This is meaningless with regards to the previous comments: if you had removed the item from the source list, it will never appear again (if it did, it was from a different position in the source list, or from a different source list overall -- and if you don't want duplicates from within a source list, you should remove them when building the source list) > slot.append(item) > a_picked_times += 1 > Given that "theLists" contains the four lists: result = [] while len(result) < 3: #using this test handles the case of sublist being empty x = random.randint(0, 99) if x < theProbabilities["A"]: clist = theLists["A"] elif x < theProbabilities["B"]: clist = theLists["B"] elif x < theProbabilities["C"]: clist = theLists["C"] else: clist = theLists["D"] if len(clist): #is sublist is empty, nothing gets appended, loop goes on result.append(random.choice(clist)) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/