s = list('some_random_string')
print s
s.reverse()
print s
s = ''.join(s)
print s
Surely there's a better way to do this, right? 10 3697
On Mon, 13 Feb 2006 18:51:11 +0000 in comp.lang.python, rtilley
<rt*****@vt.edu> wrote: s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s
Surely there's a better way to do this, right?
How about
s = "some random string"
print s
s = s[::-1]
print s
HTH,
-=Dave
-=Dave
--
Change is inevitable, progress is not.
Dave Hansen wrote: How about
s = "some random string" print s s = s[::-1] print s
That looks like Perl, but it works. Makes me wonder with the string
module doesn't have a reverse or invert function?
rtilley <rt*****@vt.edu> writes: s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s
Surely there's a better way to do this, right?
In Python 2.4, just say
s = reversed('some_random_string')
On Mon, 13 Feb 2006 19:03:32 +0000 in comp.lang.python, rtilley
<rt*****@vt.edu> wrote: Dave Hansen wrote: How about
s = "some random string" print s s = s[::-1] print s
That looks like Perl, but it works. Makes me wonder with the string module doesn't have a reverse or invert function?
It's just simple slicing. Well, maybe not so simple, or at least not
so common, but with a syntax similar to the range function. Consider
the following (string chosen to make it obvious what's going on):
s = "0123456789"
s[::]
s[3::]
s[:3:]
s[::3]
s[::-2]
s[-2::-2]
Regards,
-=Dave
--
Change is inevitable, progress is not.
Dave Hansen wrote: It's just simple slicing. Well, maybe not so simple, or at least not so common, but with a syntax similar to the range function. Consider the following (string chosen to make it obvious what's going on):
s = "0123456789" s[::] s[3::] s[:3:] s[::3] s[::-2] s[-2::-2]
Well, it turns out to be the best way to invert a string, IMO. The
reversed() feature returns a reversed object... not a reversed string.
In short, I have to fool with it again _after_ it has been inverted. The
slicing takes care of the job right away and gives me what I want... no
Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)
I'm sure the reversed feature is much more generic though for dealing
with other types.
rtilley a écrit : s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s
Surely there's a better way to do this, right?
print 'some_random_string'[::-1]
Paul Rubin a écrit : rtilley <rt*****@vt.edu> writes:
s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s
Surely there's a better way to do this, right?
In Python 2.4, just say s = reversed('some_random_string')
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information. reversed('some_random_string')
<reversed object at 0x4040f30c>
Yes, that was my first take too - but I jumped to my Python shell before
posting !-)
rtilley <rt*****@vt.edu> writes: Well, it turns out to be the best way to invert a string, IMO. The reversed() feature returns a reversed object... not a reversed string. In short, I have to fool with it again _after_ it has been inverted. The slicing takes care of the job right away and gives me what I want... no Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)
Oh, I see. I thought I'd tested reversed(...) but I guess I didn't.
I'm going senile. reversed makes an iterator.
Anyway, slicing is one solution; the array module is another.
> Well, it turns out to be the best way to invert a string, IMO. The reversed() feature returns a reversed object... not a reversed string. In short, I have to fool with it again _after_ it has been inverted. The slicing takes care of the job right away and gives me what I want... no Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)
A <reversed object> can be turned back into a string: st = '0123456789' reversed(st)
<reversed object at 0x00A8CC50> ''.join( reversed(st) )
'9876543210' I'm sure the reversed feature is much more generic though for dealing with other types.
Eric McGraw wrote: Well, it turns out to be the best way to invert a string, IMO. The reversed() feature returns a reversed object... not a reversed string. In short, I have to fool with it again _after_ it has been inverted. The slicing takes care of the job right away and gives me what I want... no Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)
A <reversed object> can be turned back into a string: >>> st = '0123456789' >>> reversed(st) <reversed object at 0x00A8CC50> >>> ''.join( reversed(st) )
'9876543210'
But that's slower than new_s = s[::-1]
Cheers,
Carl. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Des Small |
last post by:
Lately I have found myself using a pattern to make new dictionaries
quite often, by which I mean twice:
def invert(d):
nd = {}
).append(key) for k, v in d]
return nd
def count(l):
d = {}
|
by: Me Padre |
last post by:
Hoping someone can help with this. I know I can do this
programatically using a loop but I thought there might be
some easier or more effective way.
I am trying to invert an array.
i.e.
...
|
by: tdavidge |
last post by:
Folks,
Given a table that might contain the rows:
DATE ID
==== ==
1/1/2001 1A
1/1/2001 2B
1/1/2001 3C
1/1/2001 4D
|
by: osmethod |
last post by:
Hello,
I'm stuck!
I have received help before and progressed greatly from your(Access
Group) answers. I need some again please. Thanks in advance for any
suggestions.
Problem: Take the...
|
by: Peter Aitken |
last post by:
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type
ushort. I though I would use the complement operator ~ but the compiler
won't buy it. How can I do this?
Thanks,
--...
| |
by: rick |
last post by:
Why can't Python have a reverse() function/method like Ruby?
Python:
x = 'a_string'
# Reverse the string
print x
Ruby:
x = 'a_string'
# Reverse the string
|
by: mahesh.nimbalkar |
last post by:
Hi,
I have color as System.Drawing.Color c1 object as background color.
Now I would like to get another System.Drawing.Color c2 object which
is invert of c1 color to be used as foreground...
|
by: Karthik |
last post by:
Hi,
I want to record a sound wave from a mic and at the same time invert
it and play the inverted wave.My code goes as follows, however nothing
is written into the E:\inverted.wav file.Thanks in...
|
by: Stef Mientki |
last post by:
hello,
from the manual I read that a bitwise inversion should be done by invert.
But from some experiments I see that not works equally well.
Is this coincidence ?
(The disadvantage of invert...
|
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,...
|
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: 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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |