I have a lists
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
I want to iterate to access 1, 4, 7, 10, then 2, 5, 8, 11, and then 3, 6, 9, 12. How to make this iteration?
I have a lists
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
I want to iterate to access 1, 4, 7, 10, then 2, 5, 8, 11, and then 3, 6, 9, 12. How to make this iteration?
You can access them using
for i in range(len(A[0])):
for j in range(len(A)):
print(A[j][i])
If you can use something that’s not in the standard library, numpy is a good tool for modifying lists and matrices. You can do something like the following:
import numpy as np
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
print(np.asarray(A).T)