Why these don't work??

Joaquin Abian gatoygata2 at gmail.com
Thu Apr 8 22:37:16 CEST 2010


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