Python Forum
list comprehension : print column as row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list comprehension : print column as row
#4
(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
Reply


Messages In This Thread
RE: list comprehension : print column as row - by newbieAuggie2019 - Aug-22-2019, 09:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 626 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 521 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  How do you get Python to print just one value in a list? 357mag 3 1,062 May-17-2023, 09:52 PM
Last Post: rob101
  Using list comprehension with 'yield' in function tester_V 5 1,329 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,482 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Print List to Terminal DaveG 2 1,487 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Not able to add extra column to the list in the python shantanu97 2 1,698 Nov-17-2021, 10:14 AM
Last Post: snippsat
  List comprehension used differently coder_sw99 3 1,775 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,928 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,288 Jun-08-2021, 08:29 AM
Last Post: cametan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020