Re: Best way to extract an item from a set of len 1

2006-01-26 Thread Christophe
Alex Martelli a écrit : > Fredrik Lundh <[EMAIL PROTECTED]> wrote: >... > >>the obvious solution is >> >>item = list(s)[0] >> >>but that seems to be nearly twice as slow as [x for x in s][0] >>under 2.4. hmm. > > > Funny, and true on my laptop too: > > helen:~ alex$ python -mtimeit -s

Re: Best way to extract an item from a set of len 1

2006-01-26 Thread Peter Otten
Alex Martelli wrote: > Rene Pijlman <[EMAIL PROTECTED]> wrote: >> Peter Otten: >> s = set(["one-and-only"]) >> item, = s >... >> >The comma may easily be missed, though. >> >> You could write: >> >> (item,) = s >> >> But I'm not sure if this introduces additional overhead. >

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Alex Martelli
Rene Pijlman <[EMAIL PROTECTED]> wrote: > Peter Otten: > s = set(["one-and-only"]) > item, = s ... > >The comma may easily be missed, though. > > You could write: > > (item,) = s > > But I'm not sure if this introduces additional overhead. Naah...: helen:~ alex$ python -mtimei

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Alex Martelli
Fredrik Lundh <[EMAIL PROTECTED]> wrote: ... > the obvious solution is > > item = list(s)[0] > > but that seems to be nearly twice as slow as [x for x in s][0] > under 2.4. hmm. Funny, and true on my laptop too: helen:~ alex$ python -mtimeit -s's=set([23])' 'x=list(s)[0]' 10 loops,

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Fredrik Lundh
Peter Otten wrote: > > When you have a set, known to be of length one, is there a "best" > > ("most pythonic") way to retrieve that one item? > > >>> s = set(["one-and-only"]) > >>> item, = s > >>> item > 'one-and-only' > > This works for any iterable and guarantees that it contains exactly one >

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Rene Pijlman
Peter Otten: s = set(["one-and-only"]) item, = s item >'one-and-only' > >This works for any iterable and guarantees that it contains exactly one >item. Nice! >The comma may easily be missed, though. You could write: (item,) = s But I'm not sure if this introduces additional

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Alex Martelli
Tim Chase <[EMAIL PROTECTED]> wrote: ... > To get the item, i had to resort to methods that feel less than > the elegance I've come to expect from python: > > >>> item = [x for x in s][0] A shorter, clearer expression of the same idea: item = list(s)[0] or item = list(s).pop() > or

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Rene Pijlman
Tim Chase: >When you have a set, known to be of length one, is there a "best" >("most pythonic") way to retrieve that one item? e = s.copy().pop() #:-) -- René Pijlman Wat wil jij worden? http://www.carrieretijger.nl -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Fuzzyman
That's cute. :-) Fuzzyman -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Peter Otten
Tim Chase wrote: > When you have a set, known to be of length one, is there a "best" > ("most pythonic") way to retrieve that one item? >>> s = set(["one-and-only"]) >>> item, = s >>> item 'one-and-only' This works for any iterable and guarantees that it contains exactly one item. The comma may

Re: Best way to extract an item from a set of len 1

2006-01-25 Thread Fredrik Lundh
Tim Chase wrote: > I suppose I was looking for something like > > >>> item = s.aslist()[0] > > which feels a little more pythonic (IMHO). Is one solution > preferred for speed over others (as this is happening in a fairly > deeply nested loop)? the obvious solution is item = list(s)[0] but

Best way to extract an item from a set of len 1

2006-01-25 Thread Tim Chase
When you have a set, known to be of length one, is there a "best" ("most pythonic") way to retrieve that one item? # given that I've got Python2.3.[45] on hand, # hack the following two lines to get a "set" object >>> import sets >>> set = sets.Set >>> s = set(['test'])