cogmission cogmission - 1 year ago 115
Python Question

Python Special Colon Inquiry

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

Can someone tell me what is going on here?

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download