How to display matrix input values without [' ']

Asked

Viewed 82 times

0

I made an array with 12 elements that has 4 rows and columns and wanted them to be displayed like this without [' '] in list form, but I don’t want the output to be displayed like this:

Remembering: Should work with negative floats numbers and positive floats typed

Entrada:
02 04 06
08 10 12
14 16 18
20 22 24
Saída esperada:
02 04 06
08 10 12
14 16 18
20 22 24
Mas a saída está assim:
['02', '04', '06']
['08', '10', '12']
['14', '16', '18']
['20', '22', '24']
Meu código:
mt1 = input().split()
mt2 = input().split()
mt3 = input().split()
mt4 = input().split()

print (mt1)
print (mt2)
print (mt3)
print (mt4)
  • 1

    Where are you studying Python? The frequency of questions seems a little abnormal.

  • Randomly (but the biggest problem is matrix)

  • Can do with two for, dps I’ll send a version.

3 answers

3

There are several ways to do this, as can be seen in the other answers. I will leave a solution that, in my view, is quite elegant.

You may have noticed that the function print can receive several parameters, resulting in printing each of the parameters separated by a space.

print('sem', 'tempo,', 'irmão') => printa "sem tempo, irmão"

Through the asterisk (which is the operator of unpack), Python allows using the values of a list as arguments of a function.

argumentos = ['sem', 'tempo,', 'irmão']
print(*argumentos) => printa "sem tempo, irmão"

Knowing this, you can use the same technique in your code.

print(*mt1)
print(*mt2)
print(*mt3)
print(*mt4)

The result will be what you want.

If you want to put another separator other than a space, just enter in the named parameter sep.

print(*mt1, sep=' - ')

Additional reading:

2


well, I wouldn’t recommend doing so but I can help.

mt1 = input().split()
mt2 = input().split()
mt3 = input().split()
mt4 = input().split() # a função split retorna uma lista, mesmo que contendo apenas um elemento

matriz = [mt1,mt2, mt3, mt4] # já que você está trabalhando com lista junte tudo numa lista apenas 

for i in range(len(matriz)): # para varrer as linhas da matriz
    for j in range(len(matriz[i])): # para varrer todos os elementos da linha ou seja as colunas.
        print (matriz[i][j], end = "\t") # printa o elemento da linha um ao lado do outro 
    print() # dá espaço para uma nova linha.

remembering that this is not the best solution you could use a chained list, or something like.

  • That way I could understand better, thank you Filipe.

2

You can use the method .join() string. The use is

# STRING.join(LISTA)
# Exemplo:
print( ".".join(['192', '168', '0', '1']))
# result: 192.168.0.1

But in your case, just put it in the print lines:

mt1 = input().split()
mt2 = input().split()
mt3 = input().split()
mt4 = input().split()

print (' '.join(mt1))
print (' '.join(mt2))
print (' '.join(mt3))
print (' '.join(mt4))
  • 1

    I had seen the method Join by self, it worked out thank you.

  • 1

    This works with negative floats and positive floats?

  • Only works if it is a list of strings. Otherwise it returns an error. You must convert before to string with str(3.14)

Browser other questions tagged

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