Closed Thread
Results 1 to 10 of 10

Thread: 2d array not working?

  1. #1
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    2d array not working?

    Posted via CodeCall Mobile
    im trying to use a 2d array in my program, and i cant seem to find where im going wrong.
    Code:
    box=[0]*3
    box2=[box]*3
    box[0][0]=1
    box[0][0] 
    >>>[[1,0,0],[1,0,0],[1,0,0]]
    what i wanted was to assign the first rows first box with 0, not all of them.

    any help?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: 2d array not working?

    This is since all of your three arrays are actually the same one. Therefor when you change the first one all three are changed.

    Edit: Sorry I forgot to tell how to solve it. Do something like this to actually copy it:


    Code:
    box=[0]*3
    box=copy.deepcopy(box)*3
    box[0][0]=1
    I'm at school now so I can't test it but I think that will work.

  4. #3
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: 2d array not working?

    Posted via CodeCall Mobile
    thanks. i.ll try it when i can get on. Ty!

  5. #4
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: 2d array not working?

    Posted via CodeCall Mobile
    ok, tested it, same results. Have to import copy btw. Thx tho.

  6. #5
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: 2d array not working?

    Ok, I will test it when I comes home.

  7. #6
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: 2d array not working?

    Posted via CodeCall Mobile
    on a side note: ive never used deepcopy before, and i actually needed it in anotha part of the program. So thanks for the tip.

  8. #7
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: 2d array not working?

    Posted via CodeCall Mobile
    well ive done what i can for tonite, and i wont have time to get on tomoz. So thanks, and i.ll try again later.

  9. #8
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: 2d array not working?

    I solved the problem now. The problem before was that we made a copy and then added that three times instead of creating three copies. Here's the solution:



    Code:
    import copy
    
    base=[0]*3
    box=[]
    
    for i in range(0,3):
        box+=copy.deepcopy([base])
    
    box[0][0] = 1
    print box

  10. #9
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: 2d array not working?

    Posted via CodeCall Mobile
    ahhhhh. Thanks. Much appreciated.

  11. #10
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: 2d array not working?

    same thing using the whole power of Python:
    Code:
    >>> [[int(i==0) for i in range(3)] for j in range(3)]
    [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
    #with x=4 and y=5
    >>> [[int(i==0) for i in range(x)] for j in range(y)]
    [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Not working in FF/Chrome, working in IE
    By fedlerner in forum AJAX
    Replies: 17
    Last Post: 03-14-2011, 10:26 AM
  2. Export Array Contents in String Then Read Into Array Later
    By rsnider19 in forum PHP Development
    Replies: 3
    Last Post: 08-20-2010, 02:31 AM
  3. Android converting short array to byte array using java on android device.
    By maxsap in forum Mobile Development
    Replies: 0
    Last Post: 04-26-2010, 04:14 AM
  4. Replies: 0
    Last Post: 04-26-2010, 04:14 AM
  5. HTML form Array is not working in PHP
    By phplover in forum PHP Development
    Replies: 2
    Last Post: 09-08-2009, 03:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts