[Tutor] filling 2d array with zeros

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 27 20:08:39 CEST 2010


On Mon, Sep 27, 2010 at 1:54 PM, Alex Hall <mehgcap at gmail.com> wrote:
> Hi again everyone,
> I have a 2d array (I guess it is technically a list) which I want to
> fill with zeros. Later I will change some values, but any I do not
> change have to be zeros. I have two complex for loops, but I tried to
> scale things down to a couple list comprehensions and I broke things.
> What is wrong with the following line?
> self.am=[[(a,b) for a in range(len(self.lines)) a=0] for b in
> range(len(self.lines)) b=0]
>
> self.lines is another array that tells self.am how big to get, but no
> matter the size, all created cells of the self.am 2d array should end
> up with a value of 0. I barely understand list comprehensions as it is
> so am not sure what I did wrong here. I adapted this from a working
> example that fills a 2d array with numbers.
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehgcap at gmail.com; http://www.facebook.com/mehgcap
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

I don't know how you are figuring out your size, but if you note
below, you can create a list with one element value 0, then multiply
it by length you need and result is than length of array filled with
0s


 x = [0] * 4
>>> x
[0, 0, 0, 0]

-- 
Joel Goldstick


More information about the Tutor mailing list