Multiply elements of the first matrix by 3 and decrease elements of the second matrix by 3 in Python

Asked

Viewed 377 times

2

My code must have two matrices each with 5 elements and the first matrix needs to multiply its elements by 3 whereas the elements of the second matrix need to decrease their elements by 3, but it returns an error TypeError: unsupported operand type(s) for -: 'list' and 'int' caused by the line mt1, mt2 = (mt1) * 2, (mt2) - 5, how to fix this?

Entrada: 
02 03 04 05 06
10 20 30 40 50
Saída esperada: 
06  7
09  17
12  27
15  37
16  47
Meu código:

i = 0 # Linha
j = 0 # Coluna

mt1 = input().split() # Matriz 1
mt2 = input().split() # Matriz 2

mt1, mt2 = (mt1) * 2, (mt2) - 5 # PARTE ERRADA DO CÓDIGO

mt3 = zip(mt1, mt2) # Unindo as matrizes em uma com ZIP

for i, j in mt3: # linha e coluna associadas a terceira matriz
    print(i,j) # Exibe linha e coluna
  • 1

    Translation of the errro: TypeError: tipos de operandos não-suportados para -: 'list' e 'int'. That is, you cannot subtract a list from an integer or an integer from a list... What you want to do is subtract an integer for each element of the list, that is, you must iterate the list and subtract from each iterated item...

  • Yes, each element of the first matrix has to be multiplied by 3 and each element of the second matrix subtracted by 3.

  • 1

    It wasn’t a question.. I was trying to explain to you that you can’t subtract a number from a list. You can’t do it lista - 4, that doesn’t work, you should go through this list and subtract 4 of each item in the list covered. You are not doing this in your code

  • What version of your python?

  • Spyder (Python 3.6)

2 answers

5


First of all, you don’t have to declare i and j previously to use on that for.

Second, you need to convert the elements of mt1 and mt2 for int, because when you do the split it generates a list of strings. No convert to int multiplication and subtraction arithmetic operations cannot be performed.

mt1 = [int(x) for x in input().split()] # Matriz 1
mt2 = [int(x) for x in input().split()] # Matriz 2

Third, 6*3=18, that is, the last line of the expected output has to be 18 47 and not 16 47.

Now we can leave to solve the problem in fact.

the first matrix must multiply its elements by 3

You can do this already in string conversion to int.

mt1 = [int(x) * 3 for x in input().split()] # Matriz 1

the elements of the second matrix need to decrease their elements by 3

Same logic used in mt1.

mt2 = [int(x) - 3 for x in input().split()] # Matriz 2

Complete code:

mt1 = [int(x) * 3 for x in input().split()]
mt2 = [int(x) - 3 for x in input().split()]

mt3 = zip(mt1, mt2)

for i, j in mt3:
    print(i, j)
  • Great answer, only to complement that in python version 2.7 if placed only numbers and no space, for example: 01, or, 200 python converted to int. of the 3.x versions all input is string.

  • @Jonathandetoni Yes, input() in Python 2 is more or less equivalent to doing eval(input()) in Python 3. Therefore, in Python 2 the preference is to use raw_input().

  • Thank you very much for your reply

1

Using numpy operations can be performed directly with arrays and numbers:

import numpy as np

mt1 = input().split() # Matriz 1

mt1 = np.array(mt1, dtype='int') # passa o input para um numpy.array

mt1 = mt1 * 3 # realiza a operação

The necessary modification to your code would be:

import numpy as np

mt1 = np.array(input().split(), dtype='int') * 3
mt2 = np.array(input().split(), dtype='int') - 3

for i, j in zip(mt1, mt2):
    print(i, j)
  • Guy still don’t use library import in Python, more thank you anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.