-1

I'm trying to transpose a 2x3 2D array (rows become columns, vice versa). The user inputs the 6 numbers, then I have to do the rest. I'm not allowed to import any libraries. Here's what I have so far:

array1 = [[0 for column in range (2)] for row in range (3)]

for i in range (3):
    for j in range (2):
        array1[i][j] = int(input())

for i in range (3):
    for j in range (2):
        if j == 0:
            print (array1[i][j], end = " ")
        else:
            print (array1[i][j])

This code stores and prints a 2x3 2D array, any help on getting me further?

  • Does this answer your question? [Matrix Transpose in Python](https://stackoverflow.com/questions/4937491/matrix-transpose-in-python) – razdi Nov 12 '19 at 04:37
  • How about `[[mat[i][j] for i in range(len(mat))] for j in range(len(mat[0]))]` – Him Nov 12 '19 at 04:49
  • Can you mark answer as accepted if it helped you? If not, can you please leave a comment? – learner Feb 21 '20 at 08:57

1 Answers1

0

If printing is your only concern, then the following code helps:

array1 = [[0 for column in range (2)] for row in range (3)]

for i in range (3):
    for j in range (2):
        array1[i][j] = int(input())

for i in range (3):
    for j in range (2):
        if j == 0:
            print (array1[i][j], end = " ")
        else:
            print (array1[i][j])

for j in range (2):
    for i in range (3):
        if i == 0 or i == 1:
            print (array1[i][j], end = " ")
        else:
            print (array1[i][j])

However if you need the transpose of the array stored, you can use:

import numpy as np
new_array = np.array(array1)
new_array = new_array.T
learner
  • 3,168
  • 3
  • 18
  • 35