0
Как написать программу умножение матрицы на число без numpy
Моя попытка def multiply_matrix(matrix, len_x, len_y, n): # создаю матрицу new_matrix = [[0 for i in range(len_x)] for j in range(len_y)] for i in range(0,len_y): for j in range(0,len_x): # Умнажаю число из матрицы на число new_matrix[i][j] = matrix[i][j]*n return new_matrix
6 Answers
+ 2
visph ,
yes, you are right, thanks for reminding.
+ 1
Если используете двумерный массив, можно обойтись без новой матрицы и параметров длина/высота. Передайте копию массива в качестве аргумента функции и умножьте на месте.
https://code.sololearn.com/cr2401Rna3e7/?ref=app
+ 1
Vitaly Sokol
you only made a shallow copy of the 2d list... you must at least copy the content of the outer list ;)
print(multiply_([r[:] for r in matrix], 10))
+ 1
Vitaly Sokol you could also use the deepcopy function from the copy module (but you know that as you've just upvoted the post where I shared the link in another thread) ;)
0
what is your problem?
your function seems to work fine, at least if you use len_x argument as columns length (inner lists) and len_y as rows length (outer list)...
- 1
Here’s an example of how to multiply two matrices:
https://code.sololearn.com/cJd1d38NOxPe/?ref=app