By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

No built-in swap function?

P: n/a
I am looking for the most effective way to exchange v[i] and v[j], and
expected to find something like

v.swap(i, j)

but it doesn't seem to exist. Is there anything better than

v[i], v[j] = v[j], v[i]

(which I don't like particularly because it evaluates each of v[i] and v[j]
twice)?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Andrew Koenig" <ar*@acm.org> writes:
I am looking for the most effective way to exchange v[i] and v[j], and
expected to find something like

v.swap(i, j)

but it doesn't seem to exist. Is there anything better than

v[i], v[j] = v[j], v[i]

(which I don't like particularly because it evaluates each of v[i] and v[j]
twice)?


I don't think so.

It seems an odd question in one way: Saying that it evaluates v[i] and
v[j] twice may be true, but ignores the fact that v[a] is quite
different from v[a] = b -- one does a lookup (whether it be a dict
lookup, list indexing operation, or whatever), and the other is an
assignment (whether setting a dict key/val pair, setting a list item,
etc).

I admit I don't like the repetition of v[i] and v[j], though of course
that can be solved by writing a function. I suppose a function is
best here, not a method, because you might want to apply it to any
sequence.

I-am-not-a-language-lawyer-ly y'rs,
John
Jul 18 '05 #2

P: n/a
"John J. Lee" <jj*@pobox.com> wrote in message
news:ad**********@pobox.com...
It seems an odd question in one way: Saying that it evaluates v[i] and
v[j] twice may be true, but ignores the fact that v[a] is quite
different from v[a] = b -- one does a lookup (whether it be a dict
lookup, list indexing operation, or whatever), and the other is an
assignment (whether setting a dict key/val pair, setting a list item,
etc).
Of course. Nevertheless there are some operations in common, especially if
you're trying to do something like

v[i], v[i+j] = v[i+j], v[i]
I admit I don't like the repetition of v[i] and v[j], though of course
that can be solved by writing a function. I suppose a function is
best here, not a method, because you might want to apply it to any
sequence.


Perhaps. Obviously, I can write

def swap(v, i, j):
v[i], v[j] = v[j], v[i]

and then my example above becomes

swap(v, i, i+j)

However, if swap were a builtin, I think I'd expect it to be a method
because that way, it would be easier for the function to depend on the type
of v.

Anyway, it's no big deal; I just want to be sure I wasn't missing something
that's already there.
Jul 18 '05 #3

P: n/a

"Simon Wittber" <dr******@metaplay.com.au> wrote in message
news:000201c42764$5f43d3b0$0000fea9@simonxp...
And received the following output:
1 million tuple swaps in 1.78324228359 seconds.
1 million temp swaps in 1.53032270861 seconds.
1 million xor swaps in 2.17933312753 seconds.

Interestingly, using psyco.full() produced these results:
1 million tuple swaps in 3.06005958858 seconds.
1 million temp swaps in 3.01621882111 seconds.
1 million xor swaps in 3.03376686143 seconds.


I *suspect* that what this means is that Psyco adds about 50% to function
call overhead due checking types to determine which specilized
machine-coded version to call. With any substantial optimizable
computation done within the function, this is still a net win, but not when
the function body is trivial.

Terry J. Reedy


Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.