0

I want to flip a 2d list from list(2,3) to list(3,2) and shift the items diagonally:

from

a b
c d
e f

to

a c e
b d f

I managed to shift the rows and columns but now every entry is the same. meaning:

b d f
b d f

I know that has something to do with changing the original and not the a copy, but I don't know how to fix that. Can someone please help me? That is my code:

    a=0
    for array in bridge_arr:
        b=0
           for item in array:
              arr[b][a] = bridge_arr[a][b]
              b+=1
        a+=1
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

1 Answers1

0

Quite similar to a transpose of a matrix. You may use numpy. If not, you may try this transpose function:

sample = [
    [1, 2, 3],
    [4, 5, 6],
]

def transpose(data):
    return [[data[r][c] for r in range(len(data))] for c in range(len(data[0]))]

transposed_data = transpose(sample)
for row in transposed_data:
    print(row)

Output:

[1, 4]
[2, 5]
[3, 6]
Jobo Fernandez
  • 905
  • 4
  • 13