populating a doubly-subscripted array

Discussion in 'Python' started by ghw@accutrol.com, Nov 8, 2010.

  1. Guest

    What am I missing? I am using Python 3.1.2.

    ff = [[0.0]*5]*5
    ff #(lists 5x5 array of 0.0)
    for i in range(5):
    for j in range(3):
    ff[j] = i*10+j
    print (i,j,ff[j]) # correctly prints ff array values

    ff # try this and see what happens!

    result of first print of ff
    [[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]]

    result of second print of ff
    0 0 0
    0 1 1
    0 2 2
    1 0 10
    1 1 11
    1 2 12
    2 0 20
    2 1 21
    2 2 22
    3 0 30
    3 1 31
    3 2 32
    4 0 40
    4 1 41
    4 2 42

    result of third print of ff
    [[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
    [40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
    [40, 41, 42, 0.0, 0.0]]

    Obviously there is something missing in my understanding of
    how array values are populated. Why do all "rows" get
    populated with the same set of values (40, 41, 42)?

    Thanks for your help,
    Hamilton Woods
     
    , Nov 8, 2010
    #1
    1. Advertising

  2. Mark Wooding Guest

    writes:

    > What am I missing? I am using Python 3.1.2.
    >
    > ff = [[0.0]*5]*5
    > ff #(lists 5x5 array of 0.0)
    > for i in range(5):
    > for j in range(3):
    > ff[j] = i*10+j
    > print (i,j,ff[j]) # correctly prints ff array values
    >
    > ff # try this and see what happens!
    >
    > result of third print of ff
    > [[40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
    > [40, 41, 42, 0.0, 0.0], [40, 41, 42, 0.0, 0.0],
    > [40, 41, 42, 0.0, 0.0]]
    >
    > Obviously there is something missing in my understanding of
    > how array values are populated. Why do all "rows" get
    > populated with the same set of values (40, 41, 42)?


    [I suppose that the trailing 0.0 entries aren't in question: they're
    because the individual rows have width 5 -- from `[0.0]*5' -- but only
    the first three items of each is initialzed -- because j iterates only
    over range(3).]

    So, why do they all come out the same? The answer is because in fact
    there are only two lists involved. You probably thought that you were
    building something like the thing on the left. In fact, you have the
    thing on the right. (Try to forgive the rubbish ASCII art.)

    .--. .--.--.--.--.--. .--.
    | ----->| | | | | | | --.
    |__| `--^--^--^--^--' |__| \
    | | .--.--.--.--.--. | | \
    | ----->| | | | | | | ---. \
    |__| `--^--^--^--^--' |__| \v
    | | .--.--.--.--.--. | | \.--.--.--.--.--.
    | ----->| | | | | | | ----->| | | | | |
    |__| `--^--^--^--^--' |__| /`--^--^--^--^--'
    | | .--.--.--.--.--. | | / ^
    | ----->| | | | | | | --' /
    |__| `--^--^--^--^--' |__| /
    | | .--.--.--.--.--. | | /
    | ----->| | | | | | | --'
    `--' `--^--^--^--^--' `--'

    What [x] * n does is make a list, whose length is n, and all of whose
    entries are precisely the value x. If x itself is a list, then you get
    an outer list all of whose entries are the /same/ inner list -- not
    copies of it. So when you appear to change entries in one of the inner
    lists, all the other inner lists seem to change too -- because they're
    actually the same list.

    -- [mdw]
     
    Mark Wooding, Nov 8, 2010
    #2
    1. Advertising

  3. Mark Wooding Guest

    Gregory Ewing <> writes:

    > A reasonably elegant way to fix this is to use list comprehensions
    > for all except the innermost list:
    >
    > ff = [[0.0]*5 for i in xrange(5)]


    Yes, this is a good approach. I should have suggested something like
    this as a solution myself, rather than merely explaining the problem.

    > If you're computing heavily with arrays, you might also consider
    > using numpy arrays instead of lists.


    Also a good idea.

    -- [mdw]
     
    Mark Wooding, Nov 9, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andersen

    doubly linked objects

    Andersen, Oct 20, 2005, in forum: Java
    Replies:
    17
    Views:
    597
    Andrea Desole
    Nov 1, 2005
  2. murali@pune

    doubly linked list

    murali@pune, Mar 23, 2006, in forum: Java
    Replies:
    3
    Views:
    947
    bugbear
    Mar 24, 2006
  3. Zhou Yan
    Replies:
    10
    Views:
    649
    Thad Smith
    Mar 9, 2007
  4. Fabrizio Pollastri

    properties with subscripted variables

    Fabrizio Pollastri, Aug 26, 2008, in forum: Python
    Replies:
    1
    Views:
    283
    Bruno Desthuilliers
    Aug 26, 2008
  5. Replies:
    3
    Views:
    222
    Jerry Hill
    Oct 7, 2008
Loading...

Share This Page