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: -
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: -
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:" ?
3 1683 bvdet 2,851
Expert Mod 2GB
You should be using list method append() instead of list addition. This should work properly: - >>> 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. - >>> 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]]
-
>>>
"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 '+').
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 - new_matrix = []
-
for row in m:
-
new_row = []
-
for value in row:
-
new_row += [value*n]
-
new_matrix += [new_row]
-
return new_matrix
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
by: fxlogx |
last post by:
here we discuss the most basic concepts about python.
|
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.
|
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.............
|
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...
|
by: Markus Pitha |
last post by:
Hello,
I have a class Adjazenzmatrix with the following method and destructor:
private:
Adjazenzmatrix *wegmatrix;
Adjazenzmatrix *distanzmatrix;
..
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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,...
| |