Aug-22-2019, 09:06 AM
(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
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs