basic python question about lists and matrices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nervusvagus
    New Member
    • Jan 2011
    • 4

    basic python question about lists and matrices

    I'm learning python through the "How to think like a computer scientist" book. There is a certain exercise that multiplies a matrix m by a scalar n:

    Code:
    def scalar_mult(n, m):
     """
    	>>> a = [[1, 2], [3, 4]]
    	>>> scalar_mult(3, a)
    	[[3, 6], [9, 12]]
    	>>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2,2, 3]]
    	>>> b
    	[[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    	>>> scalar_mult(10, b)
    	[[30, 50, 70], [10, 10, 10], [0, 20, 0], [20,20, 30]]
    	>>> b
    	[[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    """
        new_matrix = []
        for row in m:
            new_row = []        
            for value in row:
                new_row += [value*n]
            new_matrix += [new_row]
        return new_matrix
    above is the correct code.

    Now, can someone explain why I get :


    >>> b [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    scalar_mult(10, b)

    Expected:

    [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]

    Got:

    [[30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30]]

    when the code is written as follows:
    Code:
    new_matrix = []
    new_row = [] 
        for row in m:     
            for value in row:
                new_row += [value*n]
            new_matrix += [new_row]
        return new_matrix
    what exactly happens when I move new_row = [] above "for row in m:" ?
    Last edited by bvdet; Jan 4 '11, 12:53 PM. Reason: fixed indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should be using list method append() instead of list addition. This should work properly:
    Code:
    >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    >>> def f(n, m):
    ... 	output = []
    ... 	for row in m:
    ... 		temp = []
    ... 		for value in row:
    ... 			temp.append(value*n)
    ... 		output.append(temp)
    ... 	return output
    ... 
    >>> f(10,b)
    [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
    >>>
    You can also use a list comprehension, which I prefer.
    Code:
    >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    >>> newb = [[item*10 for item in row] for row in b]
    >>> b
    [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
    >>> newb
    [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
    >>>

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      "new_row" is not declared as an empty list (zeroed) within the for() loop so it still contains all of the previous values. Add a print statement after you append the new value to see what is happening (and as bvdet said, you should be using append not '+').

      Comment

      • nervusvagus
        New Member
        • Jan 2011
        • 4

        #4
        thank you for the help, I understand better now. The append method was not in my learning level yet, the book covers it in the following chapters however the solution according to my learning level was posted here, in case anyone will be interested:


        Code:
        new_matrix = []
            for row in m:
                new_row = []        
                for value in row:
                    new_row += [value*n]
                new_matrix += [new_row]
            return new_matrix

        Comment

        Working...