81

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is

p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0])))

Now, I know that when slicing an array, the first number is start, the next is end, and the last is step, but what does the comma after the end number designate? Thanks.

Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
SolarLune
  • 1,229
  • 3
  • 12
  • 14

3 Answers3

71

It is being used to extract a specific column from a 2D array.

So your example would extract column 0 (the first column) from the first 2048 rows (0 to 2047). Note however that this syntax will only work for numpy arrays and not general python lists.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
  • 4
    thanks for specifying **this syntax will only work for numpy arrays and not general python**, I had a bit of headache trying to make it work on vanilla Python. – Alessandro De Simone Oct 19 '19 at 10:49
  • 3
    I'm confused: how a "syntax stuff" can work *only* with a package? Shouldn't the "syntax stuff" be accepted *in general* and so in Vanilla, should it? Is the package numpy modifying the interpreter / compiler? (I come from Java and C, so in my head the syntax is strongly fixed and unmovable) – Marco Ottina Jan 06 '20 at 10:29
  • 4
    It works with the package because they've implemented the relevant behavior for their `__getitem__` function. Python simply passes in the relevant arguments, but regular 2D lists won't know what to do with it. See more here https://stackoverflow.com/questions/21165751/what-does-a-colon-and-comma-stand-in-a-python-list – user976850 Feb 14 '20 at 17:56
  • To be clear, there's no syntactic difference. Python supports using any object in the brackets, and numpy is choosing to use a tuple. – Jonathon Reinhart Aug 31 '20 at 14:44
  • @user976850 does Python even have 2D lists? You can make a list of lists but that's not the same thing. – Mark Ransom Jul 01 '22 at 01:39
45

Empirically - create an array using numpy

m = np.fromfunction(lambda i, j: (i +1)* 10 + j + 1, (9, 4), dtype=int)

which assigns an array like below to m

array(
      [[11, 12, 13, 14],
       [21, 22, 23, 24],
       [31, 32, 33, 34],
       [41, 42, 43, 44],
       [51, 52, 53, 54],
       [61, 62, 63, 64],
       [71, 72, 73, 74],
       [81, 82, 83, 84],
       [91, 92, 93, 94]])

Now for the slice

m[:,0]

giving us

array([11, 21, 31, 41, 51, 61, 71, 81, 91])

I may have misinterpreted Khan Academy (so take with grain of salt):

In linear algebra terms, m[:,n] is taking the nth column vector of the matrix m

See Abhranil's note how this specific interpretation only applies to numpy

fiat
  • 15,501
  • 9
  • 81
  • 103
16

It slices with a tuple. What exactly the tuple means depends on the object being sliced. In NumPy arrays, it performs a m-dimensional slice on a n-dimensional array.

>>> class C(object):
...   def __getitem__(self, val):
...     print val
... 
>>> c = C()
>>> c[1:2,3:4]
(slice(1, 2, None), slice(3, 4, None))
>>> c[5:6,7]
(slice(5, 6, None), 7)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Okay, so I'm trying to understand - the comma is basically giving you two separate slices? EDIT: But it does this for each individual slice? Like c[5:6, 7] will return the seventh index for each fifth value in the c array (like if the fifth value in the c array was another array or list)? – SolarLune Apr 06 '12 at 21:15
  • Okay, so if I get this right, a comma will return a column of an array (in its simplest form, a 2D array, for example)? – SolarLune Apr 08 '12 at 15:03