may be a dumb question,but I can't understand why?

Fredrik Lundh fredrik at pythonware.com
Wed Nov 7 02:30:16 EST 2001


"luckey" wrote:
> the following function is in bpnn.py
> def makeMatrix(I, J, fill=0.0):
>     m = []
>     for i in xrange(I):
>         m.append([fill]*J)
>     return m
>
> I rewrite it to :
> def makeMatrix(I, J, fill=0.0):
>     m=[[fill]*J]*I
>     return m
> the result is defference
> Can someone tell me the defference between the two function?

the FAQ is your friend:

    http://www.python.org/doc/FAQ.html#4.50

    Q. My multidimensional list (array) is broken!
    What gives?

    You probably tried to make a multidimensional array
    like this.

        A = [[None] * 2] * 3

    This makes a list containing 3 references to the same
    list of length two /.../

</F>





More information about the Python-list mailing list