Re: Why these don't work??

2010-04-08 Thread nn


M. Hamed wrote:
 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:
 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/


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread Mark Dickinson
On Apr 8, 7:10 pm, M. Hamed mohammed.elshou...@microchip.com
wrote:
 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(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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread MRAB

M. Hamed wrote:

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.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread 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,

On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:
 M. Hamed wrote:
  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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread 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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread Robert Kern

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread Joaquin Abian
On Apr 8, 10:08 pm, M. Hamed mohammed.elshou...@microchip.com
wrote:
 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,

 On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:

  M. Hamed wrote:
   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.



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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread 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.

On Apr 8, 1:37 pm, Joaquin Abian gatoyga...@gmail.com wrote:
 On Apr 8, 10:08 pm, M. Hamed mohammed.elshou...@microchip.com
 wrote:



  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,

  On Apr 8, 11:43 am, MRAB pyt...@mrabarnett.plus.com wrote:

   M. Hamed wrote:
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.

 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why these don't work??

2010-04-08 Thread Lie Ryan
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).
-- 
http://mail.python.org/mailman/listinfo/python-list