By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,096 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,096 IT Pros & Developers. It's quick & easy.

basic python question about lists and matrices

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(n, m):
  2.  """
  3.     >>> a = [[1, 2], [3, 4]]
  4.     >>> scalar_mult(3, a)
  5.     [[3, 6], [9, 12]]
  6.     >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2,2, 3]]
  7.     >>> b
  8.     [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
  9.     >>> scalar_mult(10, b)
  10.     [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20,20, 30]]
  11.     >>> b
  12.     [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
  13. """
  14.     new_matrix = []
  15.     for row in m:
  16.         new_row = []        
  17.         for value in row:
  18.             new_row += [value*n]
  19.         new_matrix += [new_row]
  20.     return new_matrix  
  21.  
  22.  
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:
Expand|Select|Wrap|Line Numbers
  1. new_matrix = []
  2. new_row = [] 
  3.     for row in m:     
  4.         for value in row:
  5.             new_row += [value*n]
  6.         new_matrix += [new_row]
  7.     return new_matrix  
  8.  
what exactly happens when I move new_row = [] above "for row in m:" ?
Jan 4 '11 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You should be using list method append() instead of list addition. This should work properly:
Expand|Select|Wrap|Line Numbers
  1. >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
  2. >>> def f(n, m):
  3. ...     output = []
  4. ...     for row in m:
  5. ...         temp = []
  6. ...         for value in row:
  7. ...             temp.append(value*n)
  8. ...         output.append(temp)
  9. ...     return output
  10. ... 
  11. >>> f(10,b)
  12. [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
  13. >>> 
You can also use a list comprehension, which I prefer.
Expand|Select|Wrap|Line Numbers
  1. >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
  2. >>> newb = [[item*10 for item in row] for row in b]
  3. >>> b
  4. [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
  5. >>> newb
  6. [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
  7. >>> 
Jan 4 '11 #2

Expert 100+
P: 621
"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 '+').
Jan 4 '11 #3

P: 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:
http://en.wikibooks.org/wiki/How_to_...Solutions/CH_9

Expand|Select|Wrap|Line Numbers
  1. new_matrix = []
  2.     for row in m:
  3.         new_row = []        
  4.         for value in row:
  5.             new_row += [value*n]
  6.         new_matrix += [new_row]
  7.     return new_matrix
Jan 7 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.