Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-02 Thread Mario Garcia
This could be better: >>> import random >>> population = range(10) >>> choice = random.choice(population) >>> population.remove(choice) >>> print population >>> print population [0, 1, 2, 3, 4, 5, 6, 8, 9] That was my idea with the previous pop(), remove from the population a certain number of

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Mario Garcia
Thank you all for your input, Yes random is what I need!! I checked the docs, and follow your comments, I will use both random.sample and random.shuffle,and random.choice etc. !! For this particular problem I think ramdom.shuffle() is what I need. I was not very clear or complete in my explanati

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Carl Banks
On Jul 1, 7:02 pm, Paul Rubin wrote: > Carl Banks writes: > > random.shuffle() is still better when you're iterating through the > > whole list as the OP was doing. > The OP wrote: > >     I want to select, 70% random records from a List. I thougth set >     where a

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Paul Rubin
Carl Banks writes: > random.shuffle() is still better when you're iterating through the > whole list as the OP was doing. The OP wrote: I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: ... That sounds like 70% of the list, not the w

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Carl Banks
On Jul 1, 6:37 pm, Paul Rubin wrote: > Carl Banks writes: > > If you're iterating through the whole list and don't need to preserve > > the original order (as was the case here) random.shuffle() is better. > > 1. Random.sample avoids iterating through the whole list

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Paul Rubin
Carl Banks writes: > If you're iterating through the whole list and don't need to preserve > the original order (as was the case here) random.shuffle() is better. 1. Random.sample avoids iterating through the whole list when it can. 2. Say you want to choose 10 random numbers between 1 and 1

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Carl Banks
On Jul 1, 5:49 pm, Paul Rubin wrote: > Carl Banks writes: > > Instead, call random.shuffle() on the list, and iterate through that > > to get the elements in random order. > > It's better to use random.sample() than random.shuffle(). If you're iterating through the

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Steven D'Aprano
On Wed, 01 Jul 2009 17:49:21 -0700, Paul Rubin wrote: > Carl Banks writes: >> Instead, call random.shuffle() on the list, and iterate through that to >> get the elements in random order. > > It's better to use random.sample() than random.shuffle(). Why? -- Steven -- http://mail.python.org/m

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Paul Rubin
Carl Banks writes: > Instead, call random.shuffle() on the list, and iterate through that > to get the elements in random order. It's better to use random.sample() than random.shuffle(). -- http://mail.python.org/mailman/listinfo/python-list

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Paul Rubin
Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I No that's not a good idea. When the set/dict documentation says you get the keys in an undetermined order, it doesn't mean

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Ben Finney
Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > print c.pop() > > I was hoping to see a print

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Mensanator
On Jul 1, 5:29 pm, Mensanator wrote: > On Jul 1, 4:34 pm, Mario Garcia wrote: > > > > > > > Im trying to use sets for doing statistics from a data set. > > I want to select, 70% random records from a List. I thougth set where > > a good idea so I > > tested this way: > > > c = set(range(1000)) >

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Mensanator
On Jul 1, 4:34 pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): >      print c.pop() > > I was hoping

Re: Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Carl Banks
On Jul 1, 2:34 pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): >      print c.pop() > > I was hoping

Trying to use sets for random selection, but the pop() method returns items in order

2009-07-01 Thread Mario Garcia
Im trying to use sets for doing statistics from a data set. I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: c = set(range(1000)) for d in range(1000): print c.pop() I was hoping to see a print out of random selected numbers from 1 to 10

Re: Most pythonic way of weighted random selection

2008-08-30 Thread Steven D'Aprano
On Sat, 30 Aug 2008 17:41:27 +0200, Manuel Ebert wrote: > Dear list, > > who's got aesthetic advice for the following problem? ... [ugly code removed] > Now that looks plain ugly, and I wonder whether you might find a > slightly more elegant way of doing it without using numpy and the like. N

Re: Most pythonic way of weighted random selection

2008-08-30 Thread duncan smith
Manuel Ebert wrote: > Dear list, > > who's got aesthetic advice for the following problem? I've got some > joint probabilities of two distinct events Pr(X=x, Y=y), stored in a > list of lists of floats, where every row represents a possible outcome > of X and every float in a row a possible outcom

Re: Most pythonic way of weighted random selection

2008-08-30 Thread bearophileHUGS
Manuel Ebert, this may be related/useful: http://code.activestate.com/recipes/498229/ Note that numpy has a bisection method/function that are probably quite faster. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Most pythonic way of weighted random selection

2008-08-30 Thread Manuel Ebert
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Dear list, who's got aesthetic advice for the following problem? I've got some joint probabilities of two distinct events Pr(X=x, Y=y), stored in a list of lists of floats, where every row represents a possible outcome of X and every float in a

Re: implement random selection in Python

2007-11-19 Thread Scott David Daniels
J. Clifford Dyer wrote: > My understanding of what you are looking for is that on each individual > selection, > the probability of picking a given name is that name's prob value, divided by > the > sum of all prob values. That is, in the following case: > items = [('Mary', 96),('Shimon', 1)

Re: implement random selection in Python

2007-11-19 Thread Neil Cerutti
On 2007-11-17, Bruza <[EMAIL PROTECTED]> wrote: > OOPS. I pressed the Send too fast. > > The problem w/ Boris's solution is that after repeated calling > of randomPick(3,items), 'Jane' is not the most "frequent > appearing" member in all the out list of 3 member lists... How does this solution fai

Re: implement random selection in Python

2007-11-17 Thread Steven D'Aprano
On Sat, 17 Nov 2007 13:51:16 -0800, [EMAIL PROTECTED] wrote: > I am not guaranteeing this even works. I am seeing that there is some > collision among the numbers, but it will work for the most part. "Work for the most part" -- is that another way of saying "Apart from the bugs, this is bug-free

Re: implement random selection in Python

2007-11-17 Thread [EMAIL PROTECTED]
Knuth says to pick N distinct records from a collection where the probability is equal you should: first fill up N records by chosing the first seen. if less than N were in the collection, quit. otherwise, t = (the number of items looked at) or N to start. while your not at the end of the colle

Re: implement random selection in Python

2007-11-17 Thread J. Clifford Dyer
On Fri, 2007-11-16 at 16:47 -0800, Bruza wrote: I think I need to explain on the probability part: the "prob" is a > relative likelihood that the object will be included in the output > list. So, in my example input of > > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > So, f

Re: implement random selection in Python

2007-11-16 Thread Jordan
s really what you want. > > I can't think of any other obvious way of generalising the behaviour > of the N = 1 case. > > - Jordan > > On Nov 17, 10:50 am, Bruza <[EMAIL PROTECTED]> wrote: > > > > > On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]>

Re: implement random selection in Python

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 16:47:16 -0800, Bruza wrote: > I think I need to explain on the probability part: the "prob" is a > relative likelihood that the object will be included in the output list. > So, in my example input of > > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > S

Re: implement random selection in Python

2007-11-16 Thread duncan smith
Bruza wrote: > On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote: >> On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> >> wrote: >> >> >> >>> Bruza wrote: >>>> I need to implement a "random selection" algorithm w

Re: implement random selection in Python

2007-11-16 Thread Jordan
ROTECTED]> > > wrote: > > > > Bruza wrote: > > > > I need to implement a "random selection" algorithm which takes a list > > > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > > > likely an object, "ob

Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote: > On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> > wrote: > > > > > Bruza wrote: > > > I need to implement a "random selection" algorithm which takes a list > > > of [(obj

Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> wrote: > Bruza wrote: > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > likely an object, "obj&quo

Re: implement random selection in Python

2007-11-16 Thread duncan smith
Bruza wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probability > of > "prob".To simpl

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Boris Borcic wrote: > Bruza wrote: >> No. That does not solve the problem. What I want is a function >> >> def randomPick(n, the_items): >> >> which will return n DISTINCT items from "the_items" such that >> the n items returned are according to their probabilities specified >> in the (item, pro)

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote: > No. That does not solve the problem. What I want is a function > > def randomPick(n, the_items): > > which will return n DISTINCT items from "the_items" such that > the n items returned are according to their probabilities specified > in the (item, pro) elements inside "the_items

Re: implement random selection in Python

2007-11-16 Thread Paul Rubin
Bruza <[EMAIL PROTECTED]> writes: > But how about the general case, for N > 1 and N < len(items)? Is there > some clever algorithm using Python standard "random" package Yeah, I'm not sure what the name for it is, but there'ss a well known algorithm that's sort of an online verison of random.choic

Re: implement random selection in Python

2007-11-16 Thread bearophileHUGS
This recipe of mine may help: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498229 Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote: > No. That does not solve the problem. What I want is a function > > def randomPick(n, the_items): > > which will return n DISTINCT items from "the_items" such that > the n items returned are according to their probabilities specified > in the (item, pro) elements inside "the_items

Re: implement random selection in Python

2007-11-15 Thread Bruza
On Nov 15, 9:32 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Nov 15, 10:40�pm, Bruza <[EMAIL PROTECTED]> wrote: > > > > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Eac

Re: implement random selection in Python

2007-11-15 Thread [EMAIL PROTECTED]
On Nov 15, 10:40�pm, Bruza <[EMAIL PROTECTED]> wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probab

Re: implement random selection in Python

2007-11-15 Thread James Stroud
Bruza wrote: > I need to implement a "random selection" algorithm which takes a list > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > likely an object, "obj", should be selected based on its probability > of > "prob".To simpl

implement random selection in Python

2007-11-15 Thread Bruza
I need to implement a "random selection" algorithm which takes a list of [(obj, prob),...] as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of "prob".To simplify the problem, assuming "prob"

RE: Random selection

2007-05-21 Thread Hamilton, William
> From: Tartifola > Hi, > I have a list with probabilities as elements > > [p1,p2,p3] > > with of course p1+p2+p3=1. I'd like to draw a > random element from this list, based on the probabilities contained in > the list itself, and return its index. > > Any help on the best way to do that? > Tha

Re: Random selection

2007-05-18 Thread Roger Miller
On May 17, 10:39 pm, Tartifola <[EMAIL PROTECTED]> wrote: > Hi, > I have a list with probabilities as elements > > [p1,p2,p3] > > with of course p1+p2+p3=1. I'd like to draw a > random element from this list, based on the probabilities contained in > the list itself, and return its index. > > Any h

Re: Random selection

2007-05-18 Thread Peter Otten
Tartifola wrote: > I have a list with probabilities as elements > > [p1,p2,p3] > > with of course p1+p2+p3=1. I'd like to draw a > random element from this list, based on the probabilities contained in > the list itself, and return its index. > > Any help on the best way to do that? import ran

Random selection

2007-05-18 Thread Tartifola
Hi, I have a list with probabilities as elements [p1,p2,p3] with of course p1+p2+p3=1. I'd like to draw a random element from this list, based on the probabilities contained in the list itself, and return its index. Any help on the best way to do that? Thanks -- http://mail.python.org/mailma

Re: Weighted "random" selection from list of lists

2005-10-09 Thread Steven D'Aprano
On Sat, 08 Oct 2005 12:48:26 -0400, Jesse Noller wrote: > Once main_list is populated, I want to build a sequence from items > within the lists, "randomly" with a defined percentage of the sequence > coming for the various lists. For example, if I want a 6 item > sequence, I might want: > > 60% f

Re: Weighted "random" selection from list of lists

2005-10-08 Thread Scott David Daniels
Jesse Noller wrote: > Once main_list is populated, I want to build a sequence from items > within the lists, "randomly" with a defined percentage of the sequence > coming for the various lists. For example: > 60% from list 1 (main_list[0]), 30% from list 2 (main_list[1]), 10% from list > 3 (main_

Re: Weighted "random" selection from list of lists

2005-10-08 Thread Peter Otten
Jesse Noller wrote: > I'm probably missing something here, but I have a problem where I am > populating a list of lists like this: > > list1 = [ 'a', 'b', 'c' ] > list2 = [ 'dog', 'cat', 'panda' ] > list3 = [ 'blue', 'red', 'green' ] > > main_list = [ list1, list2, list3 ] > > Once main_list is

Re: Weighted "random" selection from list of lists

2005-10-08 Thread Ron Adam
Jesse Noller wrote: > 60% from list 1 (main_list[0]) > 30% from list 2 (main_list[1]) > 10% from list 3 (main_list[2]) > > I know how to pull a random sequence (using random()) from the lists, > but I'm not sure how to pick it with the desired percentages. > > Any help is appreciated, thanks >

Weighted "random" selection from list of lists

2005-10-08 Thread Jesse Noller
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = [ 'a', 'b', 'c' ] list2 = [ 'dog', 'cat', 'panda' ] list3 = [ 'blue', 'red', 'green' ] main_list = [ list1, list2, list3 ] Once main_list is populated, I want to build a se