-1

In the following Python syntax for the numpy library, what does the : token mean?

some_array [5, :]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • The `:` creates a `slice`. Ever used `alist[2:10:2]`? Syntactically the same thing. The actual call passes a tuple to the array indexer: `some_array.__getitem__((5, slice(None)))` – hpaulj Jan 07 '22 at 02:14
  • Welcome back to Stack Overflow. If you want to understand simple examples with a library, it is best to start with the documentation or tutorials for that library, or by [searching the Internet](https://duckduckgo.com/?q=numpy+colon). – Karl Knechtel Jan 07 '22 at 04:32
  • With all due respect, I couldn't have known the keyword column from the syntax I gave you. I had a supposition, and wanted to check that back, while engaging with the community. – theotechnic Jan 07 '22 at 13:04

1 Answers1

0

In the example that you've given, this would refer to the fifth row of some_array (assuming it's a NumPy array).

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • Yes, it's a numpy array. Why wouldn't you just write some_array [5] then? – theotechnic Jan 07 '22 at 02:00
  • Either works in this case. The notation you've described becomes useful if you want to access a column rather than a row / want every element along a given axis (e.g. a valid slice for a 3D array might look like `some_array[:,2]`, which gets the third column of a 2D array). – BrokenBenchmark Jan 07 '22 at 02:09
  • Ok, perfetto, thank you! – theotechnic Jan 07 '22 at 02:12