Why these don't work??

Robert Kern robert.kern at gmail.com
Thu Apr 8 22:36:20 CEST 2010


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




More information about the Python-list mailing list