One of the valid ways to resolve this issue is:
matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]]
matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]]
matrixTrhee = []
for l in range(4):
line = []
for c in range(3):
if matrixOne[l][c] > matrixTwo[l][c]:
line.append(matrixOne[l][c])
else:
line.append(matrixTwo[l][c])
matrixTrhee.append(line)
print(matrixTrhee)
Note that the first block for traverses the range(4) - equivalent to the number of rows in the matrix matrixTrhee. Then the second block for traverses the range(3) - equivalent to the number Dae columns of the matrix matrixTrhee - by checking whether each element of the matrixOne is greater than the corresponding element of matrixTwo. If yes, it will be added in line the element matrixOne[l][c]. Otherwise, the item matrixTwo[l][c] is added in line.
For each closure of 2nd block for is added line à matrixTrhee.
Finalizing the activities of the two blocks for the following output is displayed.
[[1, 12, 45], [85, 524, 78], [27, 63, 21], [45, 456, 962]]
Now, if you prefer to display the array in tabular form, you can use the numpy library. This way the code will be:
import numpy as np
matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]]
matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]]
matrixTrhee = []
for l in range(4):
line = []
for c in range(3):
if matrixOne[l][c] > matrixTwo[l][c]:
line.append(matrixOne[l][c])
else:
line.append(matrixTwo[l][c])
matrixTrhee.append(line)
print(np.array(matrixTrhee))
After performing all operations the following output will be displayed:
[[ 1 12 45]
[ 85 524 78]
[ 27 63 21]
[ 45 456 962]]
If you prefer you can also use List Comprehension. This way the code will stay:
import numpy as np
matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]]
matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]]
matrixTrhee = [[matrixOne[l][c] if matrixOne[l][c] > matrixTwo[l][c] else matrixTwo[l][c]
for c in range(3)] for l in range(4)]
print(np.array(matrixTrhee))
Producing the following output:
[[ 1 12 45]
[ 85 524 78]
[ 27 63 21]
[ 45 456 962]]
If there is no need to display delimiters - [] - we can use Generating Expressions. This way the code would be:
matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]]
matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]]
matrixTrhee = ((matrixOne[l][c] if matrixOne[l][c] > matrixTwo[l][c] else matrixTwo[l][c]
for c in range(3)) for l in range(4))
for i in matrixTrhee:
for j in i:
print(f'{j:>5}', end='')
print()
Getting the following output:
1 12 45
85 524 78
27 63 21
45 456 962
Or, we can simply compare the values and display the results.
matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]]
matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]]
print(f'A matrixTrhee é:')
for l in range(4):
for c in range(3):
if matrixOne[l][c] > matrixTwo[l][c]:
print(f'{matrixOne[l][c]:>5}', end='')
else:
print(f'{matrixTwo[l][c]:>5}', end='')
print()
Then we get the following output:
A matrixTrhee é:
1 12 45
85 524 78
27 63 21
45 456 962
It is important to post a question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve it. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese. Remembering also that can [Dit] the posts at any time to improve the details.
– Bacco