[ python-Feature Requests-1212091 ] sets needs an 'arbitrary element' method

SourceForge.net noreply at sourceforge.net
Fri Jun 3 22:45:10 CEST 2005


Feature Requests item #1212091, was opened at 2005-05-31 10:54
Message generated for change (Comment added) made by mkc
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1212091&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Mike Coleman (mkc)
Assigned to: Nobody/Anonymous (nobody)
Summary: sets needs an 'arbitrary element' method

Initial Comment:
It would be nice to have an "arbitrary element" method
that would return some arbitrary element of a non-empty
set (throwing an exception if the set is empty). 
Something like this

>>> s = sets.Set([1,2,3])
>>> print s.element()
2

AFAIK, the current alternative would be to do something
like this

>>> print list(s)[0]

which is somewhat expensive and unreadable.

It'd be fine if the operator returned the same or a
different element each time.  Perhaps it'd be useful if
it returned the same element each time for frozen sets.

----------------------------------------------------------------------

>Comment By: Mike Coleman (mkc)
Date: 2005-06-03 15:45

Message:
Logged In: YES 
user_id=555

I like rhettinger's "iter(s).next()" solution quite well,
except that it's kind of an obscure way to say "give me an
element of this set".  I'm completely okay with the idea
that the choose method may or may not return the same
element each time--I just want a "for example" element.

To my thinking, pop() isn't really a good solution because
it treats sets and frozensets differently.  Plus a
pop()/add() cycle seems wasteful and confusing.  One could
argue that in the other place where pop() is available
(lists), there is also a simple way to "peek" at what would
be popped, which is kind of what we're talking about here.

As for a use case, I'm not sure I have a great one.  This
came up when I was writing some union/find code.  I was
using sets to represent equivalence classes and I wanted to
be able to grab a representative of a set (which would be
any element of the set), given the set.  I was surprised to
find that there didn't seem to be a straightforward way to
do this.

I agree that I could just copy your 'choose' function into
my code easily.  There are several such functions that I
already copy into pretty much every script I write these
days (warn, error, and groupby, which probably do just what
you think they do).  If it's something that would be broadly
useful, though, it seems like it'd be a lot more efficient
for everyone to know the function by the same name (i.e., by
having it be in the standard library).





----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-05-31 11:54

Message:
Logged In: YES 
user_id=80475

I had looked at putting in a choose() method but there were
a shortage of use cases that were not better served by pop()
or by iteration.  Will leave this open as the request may
yet prove its worth.  To do so, I would need to see examples
of practical code where choose() is a better choice than
existing alternatives.

For the record, here were some of the lines of thinking
about choose(). 

* Given that any element could be returned, it was logical
to return the first encountered.  That led to the odd
situation where the method would return the same value on
every call (unless followed by a set mutation) making the
method useless in a loop. 

* If needed, an efficient alternative is available: 
iter(s).next(). And here is a more readable, encapsulated
version that a programmer could dash off in seconds:

   def choose(s, default=None):
       for elem in s:
           return elem
       return default

* I had looked at a few textbook algorithms that were
expressed in terms of choose().  Without exception, their
Python code was better expressed using pop(),  In a couple
of cases, the pop() needed to be followed by an add() if the
item was to be left in the set.  Those two cases all had
other set mutations occuring on each iteration (otherwise,
the pop/add pair would always get the same element).

----------------------------------------------------------------------

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-05-31 10:59

Message:
Logged In: YES 
user_id=1188172

For mutable sets, I think that pop() is better suited. Do
you have a use case of your proposal with frozensets?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1212091&group_id=5470


More information about the Python-bugs-list mailing list