I was wondering how you could change the user input in python into a list, or better yet, a matrix, just as you would convert it to an integer by using int(input).
Asked
Active
Viewed 4,447 times
0
-
2Why don't you want to use the zip() function? Have you seen [this question](http://stackoverflow.com/q/4937491/2001600) or [this one](http://stackoverflow.com/q/16179794/2001600)? – John Bensin Apr 24 '13 at 02:56
-
Jamylak has answered your question you need accept, [how to accept](https://stackoverflow.com/help/someone-answers) – Sep 22 '21 at 05:46
3 Answers
4
>>> L = [[1,2,3], [4,5,6], [7,8,9]]
>>> [[x[i] for x in L] for i in range(len(L[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
or
>>> zip(*L)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
or
>>> import numpy as np
>>> L = np.arange(1, 10).reshape((3, 3))
>>> L
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> L.transpose()
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])

jamylak
- 128,818
- 30
- 231
- 230
-
-
-
1@hd1 Yes I gave him the alternative but may as well put `zip` in there for other people who may view this question – jamylak Apr 24 '13 at 03:00
-
4
array([[1,2,3], [4,5,6], [7,8,9]]).T
will do what you want, if you're using numpy.

hd1
- 33,938
- 5
- 80
- 91
-
I cannot find '.T', but found numpy.transpose http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html – Timothée HENRY Dec 16 '13 at 16:14
0
list comprehensions should fit the bill quite nicely. Here's the general function:
def transpose(the_array):
return [[the_array[j][i] for j in range(0, len(the_array[i]))] for i in range(0, len(the_array))]

exists-forall
- 4,148
- 4
- 22
- 29