What Is :: (Double Colon) in Python When Subscripting Sequences

What is :: (double colon) in Python when subscripting sequences?

it means 'nothing for the first argument, nothing for the second, and jump by three'. It gets every third item of the sequence sliced.
Extended slices is what you want. New in Python 2.3

What is :: (double colon) in numpy like in myarray[0::3]?

It prints every yth element from the list / array

>>> a = [1,2,3,4,5,6,7,8,9]
>>> a[::3]
[1, 4, 7]

The additional syntax of a[x::y] means get every yth element starting at position x

ie.

>>> a[2::3]
[3, 6, 9]

Understanding slicing

The syntax is:

a[start:stop]  # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similarly to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported.
To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

print(S[::2],[1::2]) what does this line do in the below program

A double colon ::x means to skip by x. In this case it is skipping by 2.
So if you have l=[ 'a','b','c','d','e','f','g'] and you want to find l[::2], then starting at 'a', you move to 'c', and so on.

Now, if you have a number before the colons, such as x::y. This means to start at index x and skip by y.

Colon (:) in Python list index

: is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end]

[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
[len(a):] is equivalent to "from length of a to end"

Watch https://youtu.be/tKTZoB2Vjuk?t=41m40s at around 40:00 he starts explaining that.

Works with tuples and strings, too.

What does list[x::y] do?

It slices

x[startAt:endBefore:skip]

if you use skip = 2, every other element the list beginning at startAt and ending at endBefore will be selected. [Remember: indices live BETWEEN list elements]

To see this, enter

x = range(100)

at the Python prompt. Then try these things

x[::2]
x[::3]
x[10:40:6]

and see what happens.

Understanding Python's colon operator when summing arrays of multiple dimensions

Assuming depth, r and c are scalars, then

dout[depth, r, c]

is a scalar (if dout is 3d)

dout[depth, r, c] * w[depth,:,:,:]

w[depth, :, :, :] is a 3d array sliced from w, that is, the subarray selected by the depth index. This is just the scalar times each element of that subarray, producing a new array.

dx[:,r:H,c:W] += dout[depth, r, c] * w[depth,:,:,:]

is effectively:

dx[:,r:H,c:W] = dx[:, r:H, c:W] + dout[depth, r, c] * w[depth,:,:,:]

dx[:, r:H, c:W] is a slice of dx, 3d like dx but a subset along the 2nd and 3rd axes. If the slices are right, its shape should match the shape of w[depth, :,:,:]

I don't see any fancy broadcasting or special operations. It's just taking matching size parts from each of the arrays, adding them and putting the values back in the right block in dx.

The color operators are just the basic numpy indexing operators.


dx.shape  (channels, height, width)
dout.shape (num, m , k)
w.shape (num, channels, height, width)

With the 3 dimensional indexing, dout[depth, r, c] the shape of dout doesn't matter. This is just a single value.

In [295]: 10 * np.arange(12).reshape(3,4)
Out[295]:
array([[ 0, 10, 20, 30],
[ 40, 50, 60, 70],
[ 80, 90, 100, 110]])

Multiplying by a scalar can be thought of as multiplying by a matching array full of that value

In [297]: np.full((3,4),10)
Out[297]:
array([[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10]])

Broadcasting rules make it possible to do the same thing but with 1d or 2d or other size array. But I don't see that happening in your examples, I won't get into that here.



Related Topics



Leave a reply



Submit