0

Lets say I have an array

a = [[[1, 2], [3, 4]],
     [[5, 6], [7, 8]], 
     [[9, 10], [11, 12]],
     [[13, 14], [15, 16]],
     [[17, 18], [19, 20]]]

At the end I would like an array

b = [[[5, 6]],[[9, 10]]]

I thought that

b = a[1:3][0:1]

would work. But from that i get

b=[[[5, 6], [7, 8]]]
Jakob Lovern
  • 1,301
  • 7
  • 24

3 Answers3

0

so you want [[a[1][0]],[a[2][0]]? Well, for one, I'd figure out another way of writing your arrays to make them easier to parse (visually and mechanically.) But, in response to your question, I'd use a list comprehension. [a[i][0] for i in [1,2]] would work, I believe.

eugenhu
  • 1,168
  • 13
  • 22
Jakob Lovern
  • 1,301
  • 7
  • 24
0
>>> b = [[a[1][0],a[2][0]]]
>>> print(b)
[[[5, 6], [9, 10]]]
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
0

You can also see the answer to this question about transposing lists of lists, and do something like:

at = list(map(list, zip(*a)))
b = at[0][1:3]

Though as someone has mentioned in the comments, if you're going to be doing a lot of matrix manipulation, you'd be better off using NumPy.

eugenhu
  • 1,168
  • 13
  • 22