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

31st May 2021, 6:40 PM
Sting
Sting - avatar
6 Réponses
+ 2
visph , yes, you are right, thanks for reminding.
1st Jun 2021, 2:05 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Если используете двумерный массив, можно обойтись без новой матрицы и параметров длина/высота. Передайте копию массива в качестве аргумента функции и умножьте на месте. https://code.sololearn.com/cr2401Rna3e7/?ref=app
1st Jun 2021, 1:48 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 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))
1st Jun 2021, 1:54 AM
visph
visph - avatar
+ 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) ;)
1st Jun 2021, 2:19 AM
visph
visph - avatar
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)...
31st May 2021, 11:59 PM
visph
visph - avatar
- 1
Here’s an example of how to multiply two matrices: https://code.sololearn.com/cJd1d38NOxPe/?ref=app
1st Jun 2021, 6:03 PM
Per Bratthammar
Per Bratthammar - avatar