I'm reading some code and I see " list[:,i] for i in range(0,list))......"
I am mystified as to what comma is doing in there, :,
and google offers no answers as you cant google punctuation.
Any help greatly appreciated!
You are looking at numpy
multidimensional array slicing.
The comma marks a tuple, read it as [(:, i)]
, which numpy
arrays interpret as: first dimension to be sliced end-to-end (all rows) with :
, then for each row, i
selects one column.
See Indexing, Slicing and Iterating in the numpy
tutorial.
Not trying to poach Martijn's answer, but I was puzzled by this also so wrote myself a little getitem explorer that shows what's going on. Python gives a slice object to getitem that objects can decide what to do with. Multidimensional arrays are tuples too.
>>> class X(object):
... def __getitem__(self, name):
... print type(name),name
...
>>> x=X()
>>> x[:,2]
<type 'tuple'> (slice(None, None, None), 2)
>>> x[1,2,3,4]
<type 'tuple'> (1, 2, 3, 4)
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With