-1
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?

Barmar
  • 669,327
  • 51
  • 454
  • 560
cogmission
  • 107
  • 8
  • 1
    Possible duplicate of [Explain Python's slice notation](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) – DeepSpace Aug 31 '16 at 16:47
  • 1
    The magic lies in `[-numActive:]`, not in `[::-1]`. See the accepted answer in the dupe. – DeepSpace Aug 31 '16 at 16:48

2 Answers2

1

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.

Barmar
  • 669,327
  • 51
  • 454
  • 560
  • subset is the slice from -numActive to the end of winnerIndices – joel goldstick Aug 31 '16 at 16:49
  • Isn't that just another way of saying what I wrote? – Barmar Aug 31 '16 at 16:52
  • Hi thank you both for answering. So is the sorting done first or last? Because I don't see entries in the result list that are part of the last 40 entries? Which is why my assumption of what you guys said was suspect in my mind... – cogmission Aug 31 '16 at 16:56
1
winnerIndices[-numActive:]

Above takes a slice from -numActive index to the end of the original list

x[::-1]

This reverses x

Barmar
  • 669,327
  • 51
  • 454
  • 560
joel goldstick
  • 4,140
  • 6
  • 27
  • 46