Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why these don't work??

0 views
Skip to first unread message

M. Hamed

unread,
Apr 8, 2010, 2:10:51 PM4/8/10
to
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

nn

unread,
Apr 8, 2010, 2:31:13 PM4/8/10
to

reverse does not work on strings but does work on lists:
>>> x=list("abc")
>>> x.reverse()
>>> x
['c', 'b', 'a']

or maybe this:
>>> ''.join(reversed("abc"))
'cba'

as far as numpy you need to install it first:

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


Mark Dickinson

unread,
Apr 8, 2010, 2:31:47 PM4/8/10
to
On Apr 8, 7:10 pm, "M. Hamed" <mohammed.elshou...@microchip.com>
wrote:

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("abc")
<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

MRAB

unread,
Apr 8, 2010, 2:43:44 PM4/8/10
to pytho...@python.org

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. Hamed

unread,
Apr 8, 2010, 4:08:16 PM4/8/10
to
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,

Emile van Sebille

unread,
Apr 8, 2010, 4:27:45 PM4/8/10
to pytho...@python.org
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

Robert Kern

unread,
Apr 8, 2010, 4:36:20 PM4/8/10
to pytho...@python.org
On 2010-04-08 15:08 PM, M. Hamed wrote:

> 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

Joaquin Abian

unread,
Apr 8, 2010, 4:37:16 PM4/8/10
to
On Apr 8, 10:08 pm, "M. Hamed" <mohammed.elshou...@microchip.com>
wrote:

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

M. Hamed

unread,
Apr 8, 2010, 5:01:44 PM4/8/10
to
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.

Lie Ryan

unread,
Apr 8, 2010, 8:35:41 PM4/8/10
to
On 04/09/10 06:36, Robert Kern wrote:
> On 2010-04-08 15:08 PM, M. Hamed wrote:
>
>> 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).

0 new messages