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.
And that the extra-memory operation I've given above is expensive, I
believe. Is there an efficient way to do it?
--
http://mail.python.org/mailman/listinfo/python-list
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
On 28 Mar 2006 06:08:51 -0800, Sathyaish [EMAIL PROTECTED] 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
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
Sathyaish [EMAIL PROTECTED] 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.
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()
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 2005 (prerelease) (Debian 4.0.2-4)] on linux2
Type
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.)
Felipe Almeida Lessa [EMAIL PROTECTED] wrote:
Em Ter, 2006-03-28 Ã s 16:03 +0100, Sion Arrowsmith escreveu:
.join(reversed(foo))
$ python2.4 -mtimeit '.join(reversed(foo))'
10 loops, best of 3: 2.58 usec per loop
But note that a significant chunk is the join():
$ python2.4 -mtimeit
Sion Arrowsmith wrote:
But note that a significant chunk is the join():
$ python2.4 -mtimeit '.join(reversed(foo))'
10 loops, best of 3: 2.72 usec per loop
$ python2.4 -mtimeit 'reversed(foo)'
100 loops, best of 3: 1.69 usec per loop
your second benchmark doesn't do any reversal,
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
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))'
10 loops, best of 3: 6.65 usec per loop
$ python2.4 -mtimeit 'range(200)[::-1]'
10 loops, best of 3: 6.88 usec per loop
Not
14 matches
Mail list logo