Re: Probability Algorithm
On 08/25/2012 12:03 PM, 月忧茗 wrote: Hi, All, I have a problem of probability algorithm 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 ) 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% Would you like to tell us the actual assignment? This looks like it's paraphrased. if you have 3 items, each coming from one of four lists, the four probabilities have to add up to much more than 100%. Perhaps what you meant was that each of the three items had those probabilities of coming from the respective lists. Then it'd add up to 100%. Your code is far more complex than needed, and as you observed, doesn't work if each list doesn't have sufficient members. I'd simply pick a random number from 0 to 99, see if it's less than 43 and if so, use ALIST. Else if it's less than 80, use BLIST. else if it's less than 99, use CLIST. Else DLIST. Then do that 2 more times and you're done. Don't forget to factor the problem into functions, so you can easily repeat similar code. If a list is picked, and it's empty, throw an exception. Or wait till the missing item arrives. And you have to decide whether to remove the selected items from the respective lists. That wasn't specified in the problem statement. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
Sorry, missing some conditions *already_picked_list* is get from db. Why keep a counter? Rather than an iterated loop so , if use a iterated loop: for i in range(43): item = choice( ALIST ) ALIST.remove( item ) if item in already_picked_list: continue slot.append( item ) For example, if we picked item from ALIST 43 times, but all picked items are in already_picked_list What's the slot? It's a empty list, not contains item from ALIST. *This is wrong* Do you really want to remove an item from the source list? *Yes*, i*tems in slot must be unique* ( This is also a condition, which I have missing... ) and if you don't want duplicates from within a source list, you should remove them when building the source list * As I mentioned , The source list are all unique elements* Your last code. if B ,C ,D are all empty, result's elements are all from A, A's probability is 100% ? I Know , this problem should treated as two situation: If the four list has not enough elements, There are no way to ensure the probability requirements. If they has enough elements, Your solution is a good way. -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
On 08/25/2012 12:03 PM, 月忧茗 wrote: 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% First, select one of the four lists with those appropriate probabilities. Then once you selected a list, select one of its items randomly. import random def select_list(): x = random.randint(1, 100) if x = 43: return ALIST elif x = 80: # 43 + 37 return BLIST elif x = 99: # + 19 return CLIST else: return DLIST the_list = select_list() the_item = random.choice(the_list) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
Thanks for helps This code almost meets my needs . But not accurate probability when not enough source elements. So I give up the not enough elements situation. For source list is growing fast, the bast situation just appear in the program starting 2012/8/26 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info On 08/25/2012 12:03 PM, 月忧茗 wrote: 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% First, select one of the four lists with those appropriate probabilities. Then once you selected a list, select one of its items randomly. import random def select_list(): x = random.randint(1, 100) if x = 43: return ALIST elif x = 80: # 43 + 37 return BLIST elif x = 99: # + 19 return CLIST else: return DLIST the_list = select_list() the_item = random.choice(the_list) -- Steven -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list