Why these don't work??

M

M. Hamed

I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

"abc".reverse()
import numpy
 
N

nn

M. Hamed said:
I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

"abc".reverse()
import numpy

reverse does not work on strings but does work on lists:['c', 'b', 'a']

or maybe this:'cba'

as far as numpy you need to install it first:

http://pypi.python.org/pypi/numpy/
 
M

Mark Dickinson

I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

"abc".reverse()

This isn't valid Python in any version that I'm aware of. Where did
you see it? It wouldn't make a lot of sense anyway, since by analogy
with list.reverse you'd expect it to reverse the given string in
place. But that's not possible in Python, because strings are
immutable.

Maybe you're looking for something like:
<reversed object at 0x100582810>

which works in versions of Python >= 2.4.
import numpy

For this to work, you need to have numpy installed. numpy is a third-
party package that isn't part of the standard Python distribution;
for more information, see:

http://numpy.scipy.org/

The best method for installing numpy would depend on your system, and
on where you got Python from. On OS X, the system Python comes with
numpy as standard, for example. On Linux, there's probably a python26-
numpy package (or something with a similar name) that you can
install. On Windows: no idea. :)

Mark
 
M

MRAB

M. Hamed said:
I'm trying the following statements that I found here and there on
Google, but none of them works on my Python 2.5, are they too old? or
newer?

"abc".reverse()

Lists have a .reverse() method which reverses the list elements
in-place, but strings don't because they're immutable.

There's a built-in function reversed() which returns an iterator over an
iterable object, eg a string:

print reversed("abc")

for c in reversed("abc"):
print c

It's all in the documentation.
import numpy

numpy isn't part of the standard library; you'd need to download and
install it.
 
M

M. Hamed

Thanks All. That clears alot of confusion. It seems I assumed that
everything that works for lists works for strings (the immutable vs
mutable hasn't sunken in yet).

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

I had another question about arrays but I should probably start
another thread.

Regards,
 
E

Emile van Sebille

On 4/8/2010 1:08 PM M. Hamed said...
On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

map(lambda _:0, range(20))

Emile
 
R

Robert Kern

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

[0] * n

Of course, you should keep in mind that you shouldn't always be looking for
concise "built-in" expressions to do things. Or rather, you shouldn't be
disappointed if you don't find them. Almost always, the best solution is to wrap
up the ugly code into a function that you can then call everywhere. So even if
you were stuck with the list comprehension, you should have just defined your
own zeros() function that did the job and use it everywhere.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

Joaquin Abian

Thanks All. That clears alot of confusion. It seems I assumed that
everything that works for lists works for strings (the immutable vs
mutable hasn't sunken in yet).

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

I had another question about arrays but I should probably start
another thread.

Regards,

Lists have a .reverse() method which reverses the list elements
in-place, but strings don't because they're immutable.
There's a built-in function reversed() which returns an iterator over an
iterable object, eg a string:
     print reversed("abc")
     for c in reversed("abc"):
         print c
It's all in the documentation.
numpy isn't part of the standard library; you'd need to download and
install it.

if you want an array you can get it from module array
import array
array.array('i', [0]*100)
array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

if you want simply a list:
yields a list of hundred zeros

cheers
joaquin
 
M

M. Hamed

OMG! That's beautiful! I loved the [0]*n how simple and how it never
occurred to me!

Robert I agree with your comment. I feel though that since I'm not
very experienced yet with Python, it's useful to learn about all those
simple yet powerful methods so I can use them when I really need them.
Plus it gives me more justification for the time I invested learning a
new language (and glad I did), and more reasons to dump Perl forever!

Thanks for all the suggestions.

Thanks All. That clears alot of confusion. It seems I assumed that
everything that works for lists works for strings (the immutable vs
mutable hasn't sunken in yet).
On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.
I had another question about arrays but I should probably start
another thread.

if you want an array you can get it from module array
import array
array.array('i', [0]*100)

array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

if you want simply  a list:>> [0] * 100

yields a list of hundred zeros

cheers
joaquin
 
L

Lie Ryan

On the other hand (other than installing NumPy) is there a built-in
way to do an array full of zeros or one just like the numpy.zeros()? I
know I can do it with list comprehension (like [0 for i in
range(0,20)] but these are too many keystrokes for python :) I was
wondering if there is a simpler way.

[0] * n

Be careful there, that idiom only works (or only work reasonably) when
`0` is immutable (since integer is immutable, the idiom happens to work
great).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top