Results 1 to 5 of 5

Thread: Help debugging some basic python.

  1. #1
    Join Date
    Jun 2011
    Location
    Australia
    Beans
    5
    Distro
    Ubuntu 11.04 Natty Narwhal

    Help debugging some basic python.

    why is this:
    PHP Code:
        #where gap = -2
        
    for x in range(1len(matrix[0])):
            
    matrix[0][x] = matrix[0][x-1] + gap

        
    print matrix 
    returning:

    [0, -2, -4, -6, -8],
    [0, -2, -4, -6, -8],
    [0, -2, -4, -6, -8]
    instead of:

    [0, -2, -4, -6, -8],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]...ect
    It has been frustrating me for some time as I have been trying to iterate over x items in the zero-th nested list yet it iterates over x items in all lists.
    Last edited by Beauxesprits13; October 5th, 2011 at 08:00 AM.

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Help debugging some basic python.

    Quote Originally Posted by Beauxesprits13 View Post
    It has been frustrating me for some time as I have been trying to iterate over x items in the zero-th nested list yet it iterates over x items in all lists.
    Works for me:

    Code:
    >>> gap=-2
    >>> matrix=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
    >>> 
    >>> for x in range(1, len(matrix[0])):
    ...     matrix[0][x] = matrix[0][x-1] + gap
    ... 
    >>> print matrix
    [[0, -2, -4, -6, -8], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    But since your "matrix" has a two dimensions, the output should have two levels of brackets, so I suspect that you code is not really doing "print matrix" and prints the first line repeatedly.

  3. #3
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Help debugging some basic python.

    All I can say is that probably, print matrix is misindented...

    EDIT: Anyway, that's a quite weird way to iterate over the list; you could use xrange(len(matrix[0]) and not substracting 1 to x for accessing the matrix's item.
    Last edited by nvteighen; October 5th, 2011 at 09:48 PM.

  4. #4
    Join Date
    Jun 2011
    Location
    Australia
    Beans
    5
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Help debugging some basic python.

    Thanks for helping out. i think the problem was this line:

    PHP Code:
    [[0]*(len(X)+1)]*(len(Y)+1
    that i used to build my original matrix and then substitute values into. does this mean that python remembers the origanal equality of the rows in the matrix from [[0]*x]*y?

    Ffrom memory i was printing with: for row in matrix: print row

  5. #5
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Help debugging some basic python.

    Quote Originally Posted by Beauxesprits13 View Post
    Thanks for helping out. i think the problem was this line:

    PHP Code:
    [[0]*(len(X)+1)]*(len(Y)+1
    Ffrom memory i was printing with: for row in matrix: print row
    Yes what that does is create multiple references to a single object namely [0,0,0,0,0] (assuming len(X) = 3)

    so when you change anything in matrix[1] you are changing the same object as matrix[0] since each row in your matrix is the same.

    you need to populate your matrix explicitly (like ofnuts did) or do it another way - but using the * operator to create n copies of an object will actually create n copies of the referece to the object - and you will see the behaviour you see here.

    Look up the difference between shallow and deepcopy for instance.
    Last edited by Tony Flury; October 9th, 2011 at 04:12 PM.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

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
  •