Re: an element from a set

2010-05-17 Thread Steven D'Aprano
On Sun, 16 May 2010 21:53:21 -0700, Carl Banks wrote: I've never had to do it (at least not in any situations where I had any reluctance to call list on it), but it seems like a fairly bad limitation. Random element from a set is such a natural idea. There was a long discussion on the

Re: an element from a set

2010-05-17 Thread Bryan
Carl Banks wrote: [...]  Random element from a set is such a natural idea. Even if we were to modify the set type at the C level to support it, I can't think of an easy way to get a random element without selection bias.  For instance, if you start from a random hash code, some elements are

Re: an element from a set

2010-05-17 Thread Raymond Hettinger
On May 14, 3:24 pm, gerardob gberbeg...@gmail.com wrote: Hello, let S be a python set which is not empty (http://docs.python.org/library/sets.html) i would like to obtain one element (anyone, it doesn't matter which one) and assign it to a variable. How can i do this? x = next(iter(s)) or

Re: an element from a set

2010-05-17 Thread Terry Reedy
On 5/17/2010 12:53 AM, Carl Banks wrote: Even if we were to modify the set type at the C level to support it, I can't think of an easy way to get a random element without selection bias. At the C level, a (hashed) set is a list with empty slots. So the C function would have to pick random

Re: an element from a set

2010-05-16 Thread Carl Banks
On May 14, 11:52 pm, Chris Rebert c...@rebertia.com wrote: On Fri, May 14, 2010 at 11:23 PM, Carl Banks pavlovevide...@gmail.com wrote: On May 14, 9:39 am, Terry Reedy tjre...@udel.edu wrote: On 5/14/2010 11:24 AM, gerardob wrote: Hello, let S be a python set which is not empty

Re: an element from a set

2010-05-15 Thread Carl Banks
On May 14, 9:39 am, Terry Reedy tjre...@udel.edu wrote: On 5/14/2010 11:24 AM, gerardob wrote: Hello, let S be a python set which is not empty (http://docs.python.org/library/sets.html) i would like to obtain one element (anyone, it doesn't matter which one) and assign it to a

Re: an element from a set

2010-05-15 Thread Shashank Singh
On Sat, May 15, 2010 at 11:53 AM, Carl Banks pavlovevide...@gmail.comwrote: On May 14, 9:39 am, Terry Reedy tjre...@udel.edu wrote: On 5/14/2010 11:24 AM, gerardob wrote: Hello, let S be a python set which is not empty (http://docs.python.org/library/sets.html) i would like to

Re: an element from a set

2010-05-15 Thread James Mills
On Sat, May 15, 2010 at 4:23 PM, Carl Banks pavlovevide...@gmail.com wrote: Which brings up an interesting question: how do you get a random element from a set? random.choice(list(s)) is the most straightforward way and will work a lot of the time, but how would you avoid creating the list?

Re: an element from a set

2010-05-15 Thread Chris Rebert
On Fri, May 14, 2010 at 11:23 PM, Carl Banks pavlovevide...@gmail.com wrote: On May 14, 9:39 am, Terry Reedy tjre...@udel.edu wrote: On 5/14/2010 11:24 AM, gerardob wrote: Hello, let S be a python set which is not empty (http://docs.python.org/library/sets.html) i would like to obtain one

Re: an element from a set

2010-05-14 Thread Terry Reedy
On 5/14/2010 11:24 AM, gerardob wrote: Hello, let S be a python set which is not empty (http://docs.python.org/library/sets.html) i would like to obtain one element (anyone, it doesn't matter which one) and assign it to a variable. How can i do this? Depends on whether or not you want the