I have a set that contains one item. What is the best way of getting
at that item? Using pop() empties the set. Here is what I've tried.
Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
[GCC 3.3.1 (cygming special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information. from sets import Set s = Set(['foo']) s.copy().pop()
'foo' [x for x in s][0]
'foo' s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unindexable object
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker http://petef.port5.com -./\.- Hughes or their divisions. 10 1436
Pete Forman wrote: I have a set that contains one item. What is the best way of getting at that item? Using pop() empties the set. Here is what I've tried.
This is what tuple unpacking is for: s = set(['foo']) item, = s item
'foo' [item] = s item
'foo'
It's up to you whether you like the tuple or list syntax better. =)
Steve
Pete> I have a set that contains one item. What is the best way of
Pete> getting at that item? Using pop() empties the set.
Do you want to enumerate all the items in the set? If so:
for elt in s:
print elt
If you just want to grab one arbitrary (though not random) item from the
set, try:
elt = iter(s).next()
Note that repeating this operation will always return the same item: s
set(['jkl', 'foo', 'abc', 'def', 'ghi']) iter(s).next()
'jkl' iter(s).next()
'jkl' iter(s).next()
'jkl' iter(s).next()
'jkl'
Skip
Skip Montanaro <sk**@pobox.com> writes: Pete> I have a set that contains one item. What is the best way of Pete> getting at that item? Using pop() empties the set.
If you just want to grab one arbitrary (though not random) item from the set, try:
elt = iter(s).next()
I actually wanted to append the single item to a string, Steven's
solutions work for assignment.
So this looks like my best bet. I'll probably use join instead of +=
in my code. line = 'bar ' line += iter(s).next() line
'bar foo'
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker http://petef.port5.com -./\.- Hughes or their divisions.
Pete Forman wrote: I actually wanted to append the single item to a string, Steven's solutions work for assignment.
[snip] line = 'bar ' line += iter(s).next() line 'bar foo'
Yeah, using the assignment's an extra line: line_list = ['bar '] item, = s line_list.append(item) ''.join(line_list)
'bar foo'
I still tend to write the extra line in cases like this -- it guarantees
that the set is really the size that I think it is, where the
iter(s).next() solution will not raise an exception if the set is
actually larger.
Steve
Steven Bethard <st************@gmail.com> writes: I still tend to write the extra line in cases like this -- it guarantees that the set is really the size that I think it is, where the iter(s).next() solution will not raise an exception if the set is actually larger.
The preceding line in my code is
if len(s) == 1:
:-)
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker http://petef.port5.com -./\.- Hughes or their divisions.
On Wed, 24 Nov 2004 09:46:50 -0600, Skip Montanaro <sk**@pobox.com> wrote: Pete> I have a set that contains one item. What is the best way of Pete> getting at that item? Using pop() empties the set.
Do you want to enumerate all the items in the set? If so:
for elt in s: print elt
If you just want to grab one arbitrary (though not random) item from the set, try:
elt = iter(s).next()
Note that repeating this operation will always return the same item:
>>> s set(['jkl', 'foo', 'abc', 'def', 'ghi']) >>> iter(s).next() 'jkl' >>> iter(s).next() 'jkl' >>> iter(s).next() 'jkl' >>> iter(s).next() 'jkl'
Lest someone else not realize that the operation you are repeating includes creating
a fresh initialized iterator each time, and you're just doing it as a way to grab one element: s = set(['jkl', 'foo', 'abc', 'def', 'ghi']) it = iter(s) it.next()
'jkl' it.next()
'foo' it.next()
'abc' it.next()
'def' it.next()
'ghi' it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
Regards,
Bengt Richter
On Wed, 24 Nov 2004 15:40:30 GMT, Steven Bethard <st************@gmail.com> wrote: Pete Forman wrote: I have a set that contains one item. What is the best way of getting at that item? Using pop() empties the set. Here is what I've tried.
This is what tuple unpacking is for:
s = set(['foo']) item, = s item'foo' [item] = s item
'foo'
It's up to you whether you like the tuple or list syntax better. =)
Thanks. I didn't realize a list format could be used to specify target names
like that. My intial reaction is a bendy feeling in my list expression syntax
recognizer, though. I'm not sure I like that on the left hand side.
It feels too much like __setitem__ on some implied object. The tuple syntax
on the left hand side is only for unpacking (unless you want to imagine invoking
an implied unnamed function, but that's a stretch IMO), so it doesn't trigger
that near-miss syntax recognition feeling.
Regards,
Bengt Richter
Pete Forman wrote: Steven Bethard <st************@gmail.com> writes:
I still tend to write the extra line in cases like this -- it guarantees that the set is really the size that I think it is, where the iter(s).next() solution will not raise an exception if the set is actually larger.
The preceding line in my code is if len(s) == 1:
So is this just one branch of a case statement? What do you do in the
case that len(s) != 1? And which one happens more often?
If I have two possible unpackings of an iterable and I know one is much
more common than the other, I often do something like:
try:
x, y = s # more common unpacking
except ValueError:
[x], y = s, None # less common ('exceptional') unpacking
This is a reasonable pattern if your code really does favor one branch
substantially over the other. But dont' take my word for it. ;) Here's
what timeit says:
----- test.py ----
def test_cond(*args):
if len(args) == 1:
[x], y = args, None
elif len(args) == 2:
x, y = args
else:
raise ValueError('wrong number of arguments')
def test_try(*args):
try:
x, y = args
except ValueError:
[x], y = args, None
def test(fn, single_times, double_times):
for _ in range(single_times):
fn(1)
for _ in range(double_times):
fn(0, 1)
---- command prompt ----python -m timeit -s "import test" "test.test(test.test_cond, 10, 10)"
10000 loops, best of 3: 26.7 usec per loop
python -m timeit -s "import test" "test.test(test.test_try, 10, 10)"
10000 loops, best of 3: 116 usec per loop
python -m timeit -s "import test" "test.test(test.test_cond, 1, 100)"
10000 loops, best of 3: 132 usec per loop
python -m timeit -s "import test" "test.test(test.test_try, 1, 100)"
10000 loops, best of 3: 99.8 usec per loop
As you can see, when the try/except block is slower when the two
branches get traversed approximately equally, but faster when one branch
is substantially favored over the other.
Steve
Bengt Richter wrote: On Wed, 24 Nov 2004 15:40:30 GMT, Steven Bethard <st************@gmail.com> wrote:>[item] = s >item
'foo'
It's up to you whether you like the tuple or list syntax better. =)
Thanks. I didn't realize a list format could be used to specify target names like that. My intial reaction is a bendy feeling in my list expression syntax recognizer, though. I'm not sure I like that on the left hand side. It feels too much like __setitem__ on some implied object. The tuple syntax on the left hand side is only for unpacking (unless you want to imagine invoking an implied unnamed function, but that's a stretch IMO), so it doesn't trigger that near-miss syntax recognition feeling.
Yeah, I almost always prefer the tuple (comma) syntax, but occasionally
I find the list syntax clearer, if, for example, I'm unpacking a nested
single-item list: t
[['abcd'], 1, 2] (x,), y, z = t x, y, z
('abcd', 1, 2)
The ,), in the tuple-only unpacking makes me uncomfortable for some
reason. I feel marginally more comfortable with:
[x], y, z = t x, y, z
('abcd', 1, 2)
Of course, I generally feel uncomfortable if I have a weird unpacking
thing like this anyway. It pretty much only comes up for me when I want
to assign some default values in one branch of a try/except or if/else
statement, e.g.
try:
x, y = s
except ValueError:
[x], y = s, None
Steve
Steven Bethard <st************@gmail.com> writes: So is this just one branch of a case statement? What do you do in the case that len(s) != 1? And which one happens more often?
My s contains a set of possible values that are being refined. I'm
printing either a single resolved value or "many" to indicate there is
still some way to go. len(s) > 1 is not exceptional though len(s) ==
0 would be. Over the run of the program one and many will happen
equally often. Performance is less of an issue than clarity of the
code.
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent pe*********@westerngeco.com -./\.- opinion of Schlumberger, Baker http://petef.port5.com -./\.- Hughes or their divisions. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gregor Horvath |
last post by:
Hi,
>>>type()
<type 'list'>
>>>type(('1'))
<type 'str'>
I wonder why ('1') is no tuple????
|
by: Dave Veeneman |
last post by:
Is there a simple way to check the status of an item as checked or unchecked
in a CheckedListBox?
I need to cycle through a CheckedListBox and take one of two actions for
each item, depending on...
|
by: Ryan Graham |
last post by:
I totally bombed this question in an interview so I'm posting my answer here
for comments and suggestions... perhaps (god help me) I'm just not that
bright, but this works and seems to be fairly...
|
by: washoetech |
last post by:
I have a gridview control. In this grid view there is a column for the
price of an item. Some of the prices have a dollar sign in front of it and
some dont. How do I get rid of the dollar sign...
|
by: Simon Jefferies |
last post by:
Hello,
I am trying to add an item to a checked list box, like:
clbList.Items.add("Hello",true)
I get an error back:
Run-time exception thrown: System.ArgumentOutOfRangeException -...
|
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the...
|
by: jmhmaine |
last post by:
I've created a lookup class for a windows form application and I'm stuck on
how on using the collection object. The process is simple, if the item is in
the collection then return that object, if...
|
by: Michael Meckelein |
last post by:
Hello,
I run into trouble move down a selected item in a listbox. The code moving
down the item is the following one:
for (int j = lv.SelectedItems.Count-1; j >=0; j--)
{
ListViewItem...
|
by: tkpmep |
last post by:
For any list x, x.index(item) returns the index of the FIRST
occurrence of the item in x. Is there a simple way to identify the
LAST occurrence of an item in a list? My solution feels complex -...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |