473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

No built-in swap function?

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
3 3444
"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JimmyT | last post by:
I just configured and installed 2.3.4 and noticed there is no datetime module. I noticed there is a datetimemodule.c file that did not get built (ie no object file). Is there something I need to...
1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
0
by: Andrew Crook | last post by:
does MYSQL have a quota built into it! I need it limit the size of each database AndiC
1
by: Mark | last post by:
Is there a way to execute a statement that is built dynamically by a .NET application. For example I have a loop that is reading values from a database and I want to do something like the...
4
by: Yasutaka Ito | last post by:
Hi, Is there a way to determine which version of .NET Framework any given assembly is built with? thanks! -Yasutaka
1
by: William | last post by:
Looking for a pre built dotnet corporate or small business website template.
1
by: William | last post by:
Looking for a pre built dot net website for consulting business. I am trying to put up a quick business web for a dot net frame work. I have a provider already. I am trying to save time. Any...
1
by: Daniel | last post by:
is there any way to get to a unique build verion of an assembly at runtime? e.g. a version that is unique to the time that the assembly was built?
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
3
by: drewj840 | last post by:
I built a Windows service that sweeps a set of folders every 60 seconds and puts the files into a SQL Server database. I am creating a second service that will delete this set of folders and recreate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.