Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python index selection in a 3D list

I have the following 3D list:

test = [[[(x,y,z) for x in range(0,5)] for y in range(5,8)] for z in range(0,4)]
test[0].append([(0,5),(5,0)])

I want to select all the indices of the first dimension, the 0th index of the 2nd dimension and all the indices of the 3rd dimension.
If it was an array I would write array[:,0,:].
However when I write test[:][0][:] it is the same as doing test[0][:][:] which is not what I want.

How could I do that ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Transpose and take the zeroeth item.

>>> list(zip(*test))[0]
([(0, 5, 0), (1, 5, 0), (2, 5, 0), (3, 5, 0), (4, 5, 0)],
 [(0, 5, 1), (1, 5, 1), (2, 5, 1), (3, 5, 1), (4, 5, 1)],
 [(0, 5, 2), (1, 5, 2), (2, 5, 2), (3, 5, 2), (4, 5, 2)],
 [(0, 5, 3), (1, 5, 3), (2, 5, 3), (3, 5, 3), (4, 5, 3)])

[thing[0] for thing in test]

Zeroeth item in the third dimension.

 [a[0] for b in test for a in b]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading