cogmission cogmission - 3 months ago 26
Python Question

Python Special Colon Inquiry

sortedWinnerIndices = winnerIndices[-numActive:][::-1]


Can someone tell me what is going on here?

WinnerIndices
is 2048 ints long, Numpy array. I read somewhere that
[::-1]
reverses the result but I still can't figure out how this function selects a subset of winnerIndices?

Answer

Break it up into steps. It's equivalent to:

subset = winnerIndices[-numActive:]
sortedWinnerIndices = subset[::-1]

The first statement selects the last numActive elements in the array. The second line reverses it. So when you combine them you get the last numActive elements in the reverse order from the original array.