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 ?
"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
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
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 ?
--
Changes by Raymond Hettinger rhettin...@users.sourceforge.net:
--
status: open - closed
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7212
___
___
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
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
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.
--
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
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
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
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
://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
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
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
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
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
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
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
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
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
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
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
___
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
___
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
___
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,
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
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
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
___
29 matches
Mail list logo