Re: Reading a random element from a set

2017-07-26 Thread Peter Otten
ast wrote: > Hello > > random.choice on a set doesn't work because sets are > not indexable > > so I found nothing better than taking an element and > puting it back > > a = {5, 7, 8, 3, 0, 8, 1, 15, 16, 34, 765443} > elt = a.pop() > a.add(elt) > > any better idea, in a single instruction ?

Re: Reading a random element from a set

2017-07-26 Thread ast
"Steven D'Aprano" a écrit dans le message de news:597841eb$0$2878$c3e8da3$76491...@news.astraweb.com... On Wed, 26 Jul 2017 08:58:03 +0200, ast wrote: Hello random.choice on a set doesn't work because sets are not indexable so I found nothing better

Re: Reading a random element from a set

2017-07-26 Thread Steven D'Aprano
On Wed, 26 Jul 2017 08:58:03 +0200, ast wrote: > Hello > > random.choice on a set doesn't work because sets are not indexable > > so I found nothing better than taking an element and puting it back > > a = {5, 7, 8, 3, 0, 8, 1, 15, 16, 34, 765443} > elt = a.pop() > a.add(elt) That's not

Reading a random element from a set

2017-07-26 Thread ast
Hello random.choice on a set doesn't work because sets are not indexable so I found nothing better than taking an element and puting it back a = {5, 7, 8, 3, 0, 8, 1, 15, 16, 34, 765443} elt = a.pop() a.add(elt) any better idea, in a single instruction ? --

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-11-21 Thread Raymond Hettinger
Changes by Raymond Hettinger rhettin...@users.sourceforge.net: -- status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212 ___ ___

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread ipatrol
ipatrol ipatrol6...@yahoo.com added the comment: I still see a use in this. I like to use sets for lists of servers or mirrors. There is no compelling reason *not* to add a get() or pick() method, as described in http://en.wikipedia.org/wiki/Set_%28computer_science%29. Sets could be used for

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread ipatrol
ipatrol ipatrol6...@yahoo.com added the comment: I support http://bugs.python.org/msg94599 with a check to see if the length is 0, and rename it pick (based on the generic programming and mathematical literature). -- ___ Python tracker

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread Raymond Hettinger
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: Use set.pop(). Or if you don't want mutation, then use next(iter(s)) or next(iter(s),default). This technique also works for any collection including dicts and deques and whatnot. --

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 Python

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

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
://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 element removed from the set #3.1   s=set(range(10))   s {0, 1, 2, 3, 4, 5, 6

Re: an element from a set

2010-05-15 Thread Carl Banks
it to a variable. How can i do this? Depends on whether or not you want the element removed from the set #3.1   s=set(range(10))   s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}   x=next(iter(s))   x 0   s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} # x not removed   x = s.pop()   x 0   s {1, 2, 3, 4, 5, 6, 7, 8, 9} # x

Re: an element from a set

2010-05-15 Thread Shashank Singh
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 element removed from the set #3.1 s=set(range(10)) s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} x=next(iter(s)) x 0 s {0, 1

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
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 element removed from the set #3.1   s=set(range(10))   s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}   x=next(iter(s))   x 0   s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9

an element from a set

2010-05-14 Thread gerardob
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? Thanks. -- View this message in context: http://old.nabble.com/an-element-from

Re: an element from a set

2010-05-14 Thread Terry Reedy
the element removed from the set #3.1 s=set(range(10)) s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} x=next(iter(s)) x 0 s {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} # x not removed x = s.pop() x 0 s {1, 2, 3, 4, 5, 6, 7, 8, 9} # x has been removed The choice of 0 is an implementation artifact. It could have been any

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-11-05 Thread Alexander Belopolsky
Alexander Belopolsky belopol...@users.sourceforge.net added the comment: I don't want to pollute python-dev with more hopeless ideas, but I wonder if itertools could grow an efficient C-implemented def first(collection): return next(iter(collection)) On the other hand, it probably belongs

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-11-05 Thread Raymond Hettinger
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: After a long discussion on python-dev, this proposal is rejected in favor of adding documentation notes on the ways to non-destructively retrieve an arbitrary item from a set or frozenset. Here is an except from the end of

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-28 Thread Willi Richert
Willi Richert w.rich...@gmx.net added the comment: No particular reason, besides that it is ripped off of pop(). Your solution (omitting register) gives the same performance. Looks cleaner, of course. The patch tries to provide a clean way of for x in some_set: break, as explained above. See

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Changes by Willi Richert w.rich...@gmx.net: Removed file: http://bugs.python.org/file15207/setobject_get.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212 ___

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Changes by Willi Richert w.rich...@gmx.net: Added file: http://bugs.python.org/file15211/setobject_get.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212 ___

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Willi Richert w.rich...@gmx.net added the comment: added tests for get() to test_set.py -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212 ___

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Alexander Belopolsky
Alexander Belopolsky belopol...@users.sourceforge.net added the comment: Any reason you don't want to call set_next from set_get? I would say static PyObject * set_get(PySetObject *so) { register Py_ssize_t pos = 0; register setentry *entry; if (set_next(so, pos,

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Willi Richert
title: Retrieve an arbitrary element from a set without removing it type: feature request versions: Python 3.1, Python 3.2 Added file: http://bugs.python.org/file15207/setobject_get.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Raymond Hettinger
Changes by Raymond Hettinger rhettin...@users.sourceforge.net: -- assignee: - rhettinger nosy: +rhettinger versions: +Python 2.7 -Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Benjamin Peterson
Benjamin Peterson benja...@python.org added the comment: Without tests, this patch is unacceptable. -- nosy: +benjamin.peterson ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7212 ___