How would you reverse a string "in place" in python? I am seeing that
there are a lot of operations around higher level data structures and
less emphasis on primitive data. I am a little lost and can't find my
way through seeing a rev() or a reverse() or a strRev() function around
a string object.
I could traverse from end-to-beginning by using extra memory:
strText = "foo"
strTemp = ""
for chr in strText:
strTemp = chr + strTemp
but how would I do it in place?
Forget it! I got the answer to my own question. Strings are immutable,
*even* in python. Why not! The python compiler is written in C, right?
It is amazing how just writing down your problem can give you a
solution.
PS: Or, if my assumption that strings are immutable and an in-place
reversal is possible, is wrong, please correct me. 12 1690
And that the "extra-memory" operation I've given above is expensive, I
believe. Is there an efficient way to do it?
Sathyaish enlightened us with: How would you reverse a string "in place" in python?
You wouldn't, since strings are immutable.
Forget it! I got the answer to my own question. Strings are immutable, *even* in python.
Indeed :)
Why not! The python compiler is written in C, right?
Yup. But what's got that to do with it? Strings are very mutable in C.
It is amazing how just writing down your problem can give you a solution.
:)
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sathyaish wrote: And that the "extra-memory" operation I've given above is expensive, I believe. Is there an efficient way to do it?
If i recall correctly a string is an immutable list.
I would do it this way: strTXT = "foo" strREV = strTXT[::-1] strREV
'oof'
--
mph
>But what's got that to do with it? Strings are very mutable in C.
I realized after posting that I'd said something incorrect again. The
concept of "mutability" itself is a high-level concept compared to C.
Memory allocation for strings is expensive because of the way malloc()
works to find a "best-fit" against a "first-fit" in traditional memory
management systems. Because of the performance hit, high level
languages and frameworks, such as the Common Type System of the .NET
Framework for example, considers strings as immutable. That, unlike
Python, doesn't however, make them impossible to modify in-place.
Sathyaish <sa*******@gmail.com> wrote: How would you reverse a string "in place" in python? [ ... ] Forget it! I got the answer to my own question. Strings are immutable, *even* in python.
I'm not sure what that "*even*" is about, but glad that "You can't,
strings are immutable" is a satisfactory answer. Rather than writing
your own reversing code, you might like to look at: "".join(reversed("foo"))
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Sathyaish wrote: But what's got that to do with it? Strings are very mutable in C.
I realized after posting that I'd said something incorrect again. The concept of "mutability" itself is a high-level concept compared to C. Memory allocation for strings is expensive because of the way malloc() works to find a "best-fit" against a "first-fit" in traditional memory management systems. Because of the performance hit, high level languages and frameworks, such as the Common Type System of the .NET Framework for example, considers strings as immutable. That, unlike Python, doesn't however, make them impossible to modify in-place.
I believe part of the reason for their immutability is so that they can
be used as dictionary keys, which is a very common use.
Em Ter, 2006-03-28 Ć*s 16:03 +0100, Sion Arrowsmith escreveu: Rather than writing your own reversing code, you might like to look at:
"".join(reversed("foo"))
Or not:
----
$ python2.4
Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information. "".join(reversed("foo"))
'oof' "foo"[::-1]
'oof'
$ python2.4 -mtimeit '"".join(reversed("foo"))'
100000 loops, best of 3: 2.58 usec per loop
$ python2.4 -mtimeit '"foo"[::-1]'
1000000 loops, best of 3: 0.516 usec per loop
$ calc 2.58/0.516
5
----
"foo"[::-1] is cleaner and performs 5 times better -- 'nuff said.
Cheers,
--
Felipe.
On Tue, 2006-03-28 at 06:15 -0800, Sathyaish wrote: And that the "extra-memory" operation I've given above is expensive, I believe. Is there an efficient way to do it?
How big is your string?
For short strings (i.e. where large means you don't have enough RAM to
hold one extra copy.) "Abc"[::-1]
'cbA'
Also, anytime you reach for a for-loop to build a string step by step,
you are making a mistake. Consider your example.
strText = "foo"
strTemp = ""
for chr in strText:
strTemp = chr + strTemp
Each loop you are copying the string again, the timing behavior of your
function is O(n^2).
If you are really concerned about memory allocation, well, I don't know
if you realize this, but every time you call
strTemp = chr + strTemp
you are throwing away your old copy and building a new copy. Ouch.
Forgive me for preaching, but you just committed the grievous act of
premature optimization. Don't worry about that first malloc, if Python
is going to call malloc, it has a lot of opportunity to do so later.
And malloc isn't as evil as you make it out to be.
One of the advantages of using a high level language is you get to leave
the issue of how to implement the small stuff up to the language
designer and focus on the bigger picture - algorithmic appropriateness
and overall correctness.
In my experience I've found that when under time pressure python
programs tend to out perform C because doing it right is so much easier
in the former.
As for mutability, immutability is a big virtue and performance gain.
If I have two pointers to immutable strings, once I compare them I can
know for eternity which is larger, so long as I don't let go of my
references to them. Thus I can use them as keys in a complicated and
efficient data structure. If Python strings were mutable the best
implementation we could hope for dict would be a linked list.
Also, consider some other side effects of mutable strings.
s = "Abc" myfancy_structre.add_somehow( s ) t = s[::-1] print s
Abc print t
cbA
Now if strings were mutable:
s = "Abc" myfancy_structre.add_somehow( s ) s.reverseme() print s
cbA
Other users of s between assignment and reversal (like
myfancy_structure) might not be happy that is was reversed when they
next must use it.
Cheers - Adam DePrince
Felipe Almeida Lessa <fe**********@gmail.com> wrote: Em Ter, 2006-03-28 Ć*s 16:03 +0100, Sion Arrowsmith escreveu: >>> "".join(reversed("foo")) $ python2.4 -mtimeit '"".join(reversed("foo"))' 100000 loops, best of 3: 2.58 usec per loop
But note that a significant chunk is the join():
$ python2.4 -mtimeit '"".join(reversed("foo"))'
100000 loops, best of 3: 2.72 usec per loop
$ python2.4 -mtimeit 'reversed("foo")'
1000000 loops, best of 3: 1.69 usec per loop
$ python2.4 -mtimeit '"foo"[::-1]' 1000000 loops, best of 3: 0.516 usec per loop
Yeah, I forget about [::-1] due to the high profile of the introduction
of reversed(). Well, of sorted(), and reversed() coming along for the
ride. And at some point reversed() will become a win:
$ python2.4 -mtimeit 'reversed(range(200))'
100000 loops, best of 3: 6.65 usec per loop
$ python2.4 -mtimeit 'range(200)[::-1]'
100000 loops, best of 3: 6.88 usec per loop
--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Sathyaish wrote: How would you reverse a string "in place" in python? I am seeing that there are a lot of operations around higher level data structures and less emphasis on primitive data. I am a little lost and can't find my way through seeing a rev() or a reverse() or a strRev() function around a string object.
I could traverse from end-to-beginning by using extra memory:
strText = "foo" strTemp = "" for chr in strText: strTemp = chr + strTemp
but how would I do it in place?
Forget it! I got the answer to my own question. Strings are immutable, *even* in python. Why not! The python compiler is written in C, right? It is amazing how just writing down your problem can give you a solution.
PS: Or, if my assumption that strings are immutable and an in-place reversal is possible, is wrong, please correct me.
If you are using strings that are long enough where you're going to run
into memory issues, you can create a character array from the array
module. This will basically create a mutable string.
Sion Arrowsmith wrote: But note that a significant chunk is the join():
$ python2.4 -mtimeit '"".join(reversed("foo"))' 100000 loops, best of 3: 2.72 usec per loop $ python2.4 -mtimeit 'reversed("foo")' 1000000 loops, best of 3: 1.69 usec per loop
your second benchmark doesn't do any reversal, though. it only
creates a bunch of reversed() iterator objects.
it's a little like my old faster-than-the-speed-of-light XML parser
benchmark:
dir test.xml
....
2005-05-04 20:41 12 658 399 test.xml
.... python -m timeit -s "import cElementTree" "matches = (elem.get('value')
for event, elem in cElementTree.iterparse('test.xml') if elem.get('name')
== 'reselectApi')"
1000 loops, best of 3: 198 usec per loop python -c "print 12658399 / 198e-6"
63931308080.8
(64 gigabytes per second? who said XML was a performance hog ?)
</F>
Em Ter, 2006-03-28 Ć*s 17:32 +0100, Sion Arrowsmith escreveu: ride. And at some point reversed() will become a win:
$ python2.4 -mtimeit 'reversed(range(200))' 100000 loops, best of 3: 6.65 usec per loop $ python2.4 -mtimeit 'range(200)[::-1]' 100000 loops, best of 3: 6.88 usec per loop
Not fair:
$ python2.4
Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information. range(200)[::-1]
[199, 198, ..., 1, 0] reversed(range(200))
<listreverseiterator object at 0xb7d1224c> list(reversed(range(200)))
[199, 198, ..., 1, 0] list(reversed(range(200))) == range(200)[::-1]
True^D
Now we're in a fair competition:
$ python2.4 -mtimeit -s 'a=range(200)' 'a[::-1]'
100000 loops, best of 3: 2.23 usec per loop
$ python2.4 -mtimeit -s 'a=range(200)' 'list(reversed(a))'
100000 loops, best of 3: 5.62 usec per loop
$ calc 5.62/2.23
~2.52017937219730941704
$ python2.4 -mtimeit -s 'a=range(200000)' 'a[::-1]'
100 loops, best of 3: 10.7 msec per loop
$ python2.4 -mtimeit -s 'a=range(200000)' 'list(reversed(a))'
100 loops, best of 3: 11.5 msec per loop
$ calc 11.5/10.7
~1.07476635514018691589
But:
$ python2.4 -mtimeit 'range(199, -1, -1)'
100000 loops, best of 3: 4.8 usec per loop
$ python2.4 -mtimeit 'range(200)[::-1]'
100000 loops, best of 3: 7.05 usec per loop
$ calc 7.05/4.8
1.46875
$ python2.4 -mtimeit 'range(199999, -1, -1)'
100 loops, best of 3: 13.7 msec per loop
$ python2.4 -mtimeit 'range(200000)[::-1]'
10 loops, best of 3: 24 msec per loop
$ calc 24/13.7
~1.75182481751824817518
And worse:
$ python2.4 -mtimeit 'list(reversed(range(200)))'
100000 loops, best of 3: 10.5 usec per loop
$ calc 10.5/4.8
2.1875
$ python2.4 -mtimeit 'list(reversed(range(200000)))'
10 loops, best of 3: 24.5 msec per loop
$ calc 24.5/13.7
~1.78832116788321167883
HTH,
--
Felipe. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: JS Bangs |
last post by:
I started using PHP's object-oriented stuff a little while ago, which has
mostly been a joy. However, I've noticed that they don't seem to echo as I
would like. Eg:
$this->field = 255;...
|
by: Ben Eisenberg |
last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
|
by: James |
last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP
?
Where ID = $ID !
Any examples or URLS ?
Thanks
|
by: Patrick Schlaepfer |
last post by:
Why this code is not working on Solaris 2.8 host. Always
getting:
PHP Fatal error: swfaction() :
getURL('http://www.php.net'
^
Line 1: Reason: 'syntax error'
in /.../htdocs/ming2.php on...
|
by: phpkid |
last post by:
Howdy
I've been given conflicting answers about search engines picking up
urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3
Do search engines pick up these urls?
I've been considering...
| |
by: lawrence |
last post by:
What is the PHP equivalent of messaging, as in Java?
|
by: Quinten Carlson |
last post by:
Is there a way to conditionally define a function in php?
I'm trying to run a php page 10 times using the include statement, but I get
an error because my function is already defined. The docs...
|
by: lawrence |
last post by:
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and...
|
by: Phillip Wu |
last post by:
Hi,
I saw a previous post about sending arrays but did not quite
understand the answers.
The problem is that I would like to pass an entire array as a hidden
input field from one php script...
|
by: Matt Schroeder |
last post by:
Does anyone know how to count how many rows are in a mysql table? This is
what I have, but it doesn't work right:
<?
$db = mysql_connect("localhost", "username", "password");...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |