60

I'm new to Python and numpy. I've figured out how to slice 1 dimensional sequence: arr[start:end], and access an element in the array: el = arr[row][col].

Trying something like slice = arr[0:2][0:2] (where arr is a numpy array) doesn't give me the first 2 rows and columns, but repeats the first 2 rows. What did I just do, and how do I slice along another dimension?

SlightlyCuban
  • 3,045
  • 1
  • 19
  • 31

1 Answers1

86

If you use numpy, this is easy:

slice = arr[:2,:2]

or if you want the 0's,

slice = arr[0:2,0:2]

You'll get the same result.

*note that slice is actually the name of a builtin-type. Generally, I would advise giving your object a different "name".


Another way, if you're working with lists of lists*:

slice = [arr[i][0:2] for i in range(0,2)]

(Note that the 0's here are unnecessary: [arr[i][:2] for i in range(2)] would also work.).

What I did here is that I take each desired row 1 at a time (arr[i]). I then slice the columns I want out of that row and add it to the list that I'm building.

If you naively try: arr[0:2] You get the first 2 rows which if you then slice again arr[0:2][0:2], you're just slicing the first two rows over again.

*This actually works for numpy arrays too, but it will be slow compared to the "native" solution I posted above.

mgilson
  • 283,004
  • 58
  • 591
  • 667
  • So, what does `arr[0:2][0:2]` actually do compared to `arr[row][col]`? – SlightlyCuban Jun 24 '13 at 13:53
  • 2
    @mgilson I tired this and got some rather strange result. Suppose I had `a=[[1,2,3],[4,5,6],[7,8,9]]` then `a[0][:]=[1, 2, 3]` but strangely enough `a[:][0]` is still `[1,2,3]`. I would say that this should be `[1,4,7]`. I would appreciate if you could tell me what is wrong here. – Alexander Cska Apr 13 '16 at 18:48
  • 4
    @AlexanderCska That's not that strange actually. Those two expressions are basically "Take the first element of `a` and return a copy of it" and "Copy `a` and return the first element of the copy". The fix is `[x[0] for x in a]`, or, if you're working with `numpy`, `a[:, 0]` – mgilson Apr 13 '16 at 19:46
  • 1
    @mgilson OK, I was trying to apply FORTRAN notation to python and I see now that things are a bit more involved. – Alexander Cska Apr 16 '16 at 21:26
  • @AlexanderCska -- If you are using `numpy`, they're actually remarkably similar... Without numpy, it's quite different (more C-like in the multidimensional array is an "array of pointers to arrays"). – mgilson Apr 17 '16 at 03:10
  • 1
    @mgilson I get it now, under normal circumstances `a[:]` is a copy and is used to avoid having multiple pointers referencing the same memory location. I just confused `C` and python syntax. – Alexander Cska May 11 '16 at 09:24