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
#1
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 ?
Reply
#2
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 500 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 462 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,006 May-17-2023, 09:52 PM
Last Post: rob101
  Using list comprehension with 'yield' in function tester_V 5 1,242 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,387 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Print List to Terminal DaveG 2 1,430 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Not able to add extra column to the list in the python shantanu97 2 1,646 Nov-17-2021, 10:14 AM
Last Post: snippsat
  List comprehension used differently coder_sw99 3 1,708 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,818 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,232 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