Multiplication of arrays in Python

0

Hello, I'm doing an algorithm in python that makes the product between two arrays, but I'm having trouble displaying the resulting array

matriz = []
linha = []
linha2= []
matriz_result = []
result = 0
lista = []
nlin,ncol= map(int,input().split())
for i in range(0,nlin):
 valor = (input().split())
 valor = list(map(int,valor))
 linha.append(valor)
 matriz.append(linha)


matriz2 = []
nlin2,ncol2 = map(int,input().split())
for i in range(0,nlin2):
 valor = (input().split())
 valor = list(map(int,valor))
 linha2.append(valor)
 matriz2.append(linha2)

for i in range(nlin):
 if ncol!=nlin2:
     print("ERRO")
 for j in range(ncol):
      for h in range(0,1):
           for k in range(ncol):
                result += matriz[i][i][k]*matriz2[k][k][i]
                matriz_result.append(result)



print(matriz_result)

I have to put the array entries on the same line, so for some reason an array element is given by three parameters.

Does anyone know what I need to change in the code?

    
asked by anonymous 28.11.2016 / 03:26

1 answer

0

Hand, when you asked the user to insert the list you put the line in the value variable, then it transforms into a list and then puts it inside another list, so it gets an array with 3 indexes.

Direct to the line variable.

for i in range(0,nlin):
 linha = (input().split())
 linha = list(map(int,linha))
 matriz.append(linha)

But the Resultant Matrix is not just with the elements, it shows all the factors of each element.

And you're not calculating factors either, the next factor is always the sum of the above factors.

In addition to for j in range(ncol): must be ncol2 getting for j in range(ncol2): because in for i in range(nlin): is defined the amount of rows of the resulting array that will be the same amount of rows in array 1 and in for j in range(ncol): will be defined the amount of columns of the resulting array that will be the same amount of columns of array 2 and not 1 as represented.

The for k in range(ncol): could also be written with nlin2 but not with nlin nor with ncol2 because in this is defined the amount of factors that added will result in each element of the resulting matrix and that amount of factors is the same as the value of the columns of array 1 and the lines of array 2.

This for h in range(0,1): is also unnecessary, because of the way it is going to run only once but all code will run at least once.

In this site Page in ScriptBrasil shows a correct way to multiply arrays.

    
14.10.2017 / 22:02