Why these don't work??

M. Hamed mhelshou at hotmail.com
Thu Apr 8 17:01:44 EDT 2010


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... at gmail.com> wrote:
> On Apr 8, 10:08 pm, "M. Hamed" <mohammed.elshou... at 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... at 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




More information about the Python-list mailing list