473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:

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
3 1683
bvdet
2,851 Expert Mod 2GB
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
dwblas
626 Expert 512MB
"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
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

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

Similar topics

6
by: Jimmy Clay | last post by:
I have a very basic question about how to include files to be used by another file. I'm trying to include a bad word filter into the guestmap on my website. I know almost nothing about PHP code....
3
by: Paul K | last post by:
I have a very basic question about ref and variables If I create a variable such a private Form1 mform and then assign mform a variable passed by reference such a Form2(ref Form1 f){mform...
7
by: fxlogx | last post by:
here we discuss the most basic concepts about python.
8
by: sreekandan | last post by:
Hi everybody, Im doing the project to maintain the Accounts of an organization.So I want the basic ideas about the Accounts such as employee details, salary details.........So kindly reply me.
1
by: Lastknight | last post by:
hi everyone give some basic tips about how to understand oops in perl and how to build a package by using it.............
1
by: Chaven | last post by:
Hi Ive done some work on a basic python editor, with code folding, syntax highlighting and the idle shell. It also includes a forms editor to quickly build windows forms applications using...
7
by: Markus Pitha | last post by:
Hello, I have a class Adjazenzmatrix with the following method and destructor: private: Adjazenzmatrix *wegmatrix; Adjazenzmatrix *distanzmatrix; ..
6
by: simplicity | last post by:
I have some basic questions about alert() and confirm(): (1) Is it possible to customize the labels in the buttons, eg. put "Yes" instead of "OK"? (2) Is there a control over the text in the...
6
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.