Python Forum

Full Version: list comprehension : print column as row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a 9 x 6 matrix (each value is a string) and need to print columns as rows, which is 6 x 9 matrix.

So, when I execute the following code it just prints the same without the strings.
No matter what I try, it's just does not work.

Here is the code : so when I just print row[0], it prints the row as column, but it doesn't work using the nested for loop

for row in grid:
    # print(row[0], end=" ")
    for col in row:
        print(col, end=" ")
    print()
Any thoughts and ideas, please ?
One simple way is to unpack matrix, zip it and then unpack zip-object:

>>> m = [[1, 2], [3, 4]]
>>> print(*zip(*m), sep='\n')
(1, 3)
(2, 4)
Zip takes iterables and return a object. For each iteration on the zip object, you get one element from the iterables.
If the iterables in this form:
matrix = [
    ['row0_col0', 'row_0_col1', 'row_0col2'],
    ['row1_col0', 'row_1_col1', 'row_1col2'],
    ['row2_col0', 'row_2_col1', 'row_2col2'],
    ['row3_col0', 'row_3_col1', 'row_3col2'],
]
The * in front of the iterable, unpacks it. Then the zip function will get following:

zip(*martix) # ->
zip(
    ['row0_col0', 'row_0_col1', 'row_0col2'],
    ['row1_col0', 'row_1_col1', 'row_1col2'],
    ['row2_col0', 'row_2_col1', 'row_2col2'],
    ['row3_col0', 'row_3_col1', 'row_3col2'],
)
Making use of it:
zip_object = zip(*martix)
print(next(zip_object))
Output:
('row0_col0', 'row1_col0', 'row2_col0', 'row3_col0')
This procedure is well known as transpose. So if some who has no knowledge about programming, but has knowledge about Excel, understands directly what transpose is.
(Aug-22-2019, 06:12 AM)pyseeker Wrote: [ -> ]I have a 9 x 6 matrix (each value is a string) and need to print columns as rows, which is 6 x 9 matrix.

So, when I execute the following code it just prints the same without the strings.
No matter what I try, it's just does not work.

Here is the code : so when I just print row[0], it prints the row as column, but it doesn't work using the nested for loop

for row in grid:
    # print(row[0], end=" ")
    for col in row:
        print(col, end=" ")
    print()
Any thoughts and ideas, please ?

Hi!

I'm a newbie, so I don't know yet about zip and other things.
I just tried to look for a solution with a nested for loop, similar to the one you showed in your question. So I made this little program to show the differences among the original matrix, its output on screen, and the transposed matrix:

print("\nThis is matrix m1 (9x6):\n")
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print(m1)

print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
    for j in range(len(m1[i])):
        print(m1[i][j], end=' ')
    print()


print("\n\nBuilding matrix m2 (6x9):\n")
for i in range(0, 6):
    for j in range(0, 9):
        print(m1[j][i], end=' ')
    print()
This gives the following output:
Output:
This is matrix m1 (9x6): [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']] Building matrix m1 (9x6): 00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85 Building matrix m2 (6x9): 00 10 20 30 40 50 60 70 80 01 11 21 31 41 51 61 71 81 02 12 22 32 42 52 62 72 82 03 13 23 33 43 53 63 73 83 04 14 24 34 44 54 64 74 84 05 15 25 35 45 55 65 75 85
I hope it helps,

newbieAuggie2019
Perfect, Thank you very much, works like charm.
Lately, I have solved to not printing the original matrix, something like :


for row in range(len(matrix)):
    print(row)
for col in range(len(matrix[i])):
    print(matrix([col][row], end=" ")
print()