Home > front end >  Python index selection in a 3D list
Python index selection in a 3D list

Time:02-01

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 ?

CodePudding user response:

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]
  • Related