I want to do a 2X3 transposition in python but can't
Question:
matrix = [['*','*','*'],
['*','*','*']]
t_matrix = [['*','*'],
['*','*'],
['*','*']]
print(list(zip(*matrix)))
[('*', '*'), ('*', '*'), ('*', '*')]
The above is what happens.
I want the matrix to look like t_matrix, but it doesn’t. How can I do the transposition?
Answers:
Just use a list comprehension to map each tuple to a list.
print([[a, b] for a, b in zip(*matrix)])
More generally:
print([list(x) for x in zip(*matrix)])
matrix = [['*','*','*'],
['*','*','*']]
t_matrix = [['*','*'],
['*','*'],
['*','*']]
print(list(zip(*matrix)))
[('*', '*'), ('*', '*'), ('*', '*')]
The above is what happens.
I want the matrix to look like t_matrix, but it doesn’t. How can I do the transposition?
Just use a list comprehension to map each tuple to a list.
print([[a, b] for a, b in zip(*matrix)])
More generally:
print([list(x) for x in zip(*matrix)])