Join two matrices and display them in a third vertically

Asked

Viewed 250 times

0

I created two M1 and M2 matrices and they have a dimension for seven elements. A third matrix called M3 has two dimensions where its first column must be formed by the elements of matrix MA and the second column by the elements of matrix B. Right after you need to display M3:

input:
02 04 06 08 10 12 14
02 04 06 08 10 12 14
Saída
02  02
04  04
06  06
08  08
10  10
12  12
14  14
Código:
M1 = input()
lista1 = M1.split()
M2 = input()
lista2 = M2.split()

MA = [lista1]
MB = [lista2]

print (MA)
print (MB)
  • 3

    Something like M3 = [[a, b] for a, b in zip(M1, M2)]?

  • It gave a result, only it’s not quite as expected >>> [['1', '1'], [' ', ' '], ['2', '2'], [' ', ' '], ['3', '3'], [' ', ' '], ['4', '4'], [' ', ' '], ['5', '5'], [' ', ' '], ['6', '6'], [' ', ' '], ['7', '7']]

  • Lucas, this happened because in your M1 and M2 code are not matrices of a dimension as described in the question. In the code, are lista1 and lista2.

2 answers

0

Lucas, the answer is as our colleague Anderson pointed out, yet when you put the condition var split.() you turn it into a vector.

Test under the conditions I have placed below and edit as needed:

m1 = []
for i in range(7):
    m1.append(int(input()))

m2 = m1.copy()

m3 = [[a,b] for a, b in zip(m1, m2)]

for row in m3:
    print(row[0], row[1])
  • m2 = M1.copy() >> Syntaxerror: invalid syntax

  • I suggest you set the full example, with data acquisition for the two lists - and run your code to test. Python lists do not have the "copy" method, for example - (but this would be an "Attributeerror", as you have not tested your code, yet it lacked close a parenthesis in the line above this)

  • @Lucaspimentel, the Syntaxerror is precisely by the lack of the ) in the m1.append(int(input())**)** It should also work properly. @jsbueno the example is complete, and I also performed the shell test before to after encoding in a script to post them. However my version is Python 3.7, which already has the "copy" method, follows the list documentation link. https://docs.python.org/3.7/tutorial/datastructures.html#Functional-Programming-tools

  • It was different from the expected input and output, there is no way to leave exactly as I specified?

  • @Lucaspimentel, realized the edition, execute this way it will be the same as requested.

0

The two matrices received in the input form a 2x7 matrix, and the result you want to get is a transposition 2x7 matrix. This is simple to do in Python.

m1 = input().split()
m2 = input().split()

m3 = zip(m1, m2)

for a, b in m3:
    print(a, b)

Just out of curiosity, it follows the same algorithm written in a single line.

print(*[f'{a} {b}' for a, b in zip(input().split(), input().split())], sep='\n')
  • For me to leave all the code in one line is more complicated than to leave it in several lines kkk.

  • It worked, thanks for your help.

  • The code has the correct result, I just did not understand why it is mentioned matrix transposition since there is no transposition, because the lists are not changed and also there is no 2x7 matrix. He just wants a new matrix with the elements of the interspersed lists...

  • @fernandosavio "there is no transposition as the lists are not changed" The output of the program is the transposed matrix. The transposition does not happen in-place because it is not necessary. If you want to check, access this site, create a 2x7 matrix equal to what the OP reported and see the result.

  • I wanted to mention that there are two 1x7 matrices, and that you don’t explain very well what that has to do with. You claim there is transposition and show a code without developing the explanation. That’s why I said the code is correct but the explanation doesn’t seem to be related to the code, you can argue and explain everything in the comments, but this explanation would be better in your answer, you know? His explanation lacks development

Browser other questions tagged

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