0
[b for _, b in sorted(enumerate(arr, 1), key=lambda x: int.__mul__(*x))]

is a list comprehension in Python meant to sort an array in ascending order while using the value at each place multipled by a 1-based index

So [4, 3, 1] would be sorted based on [4*1, 3*2, 1*3] giving [1, 4, 3].

I am trying to understand: 1. How the _ in the code works 2. How the *x multiplies the value with its index (Since the enumerate has 1 as a parameter, I am assuming that is how the 1-based indexing is taken care of).

Starlight
  • 111
  • 1
  • 2
  • 2
    `_` is just a regular variable name, intended to hint that the value being stored in it is unused. `int.__mul__(*x)` is a really stupid way of writing `x[0] * x[1]`. – Ry- Apr 06 '17 at 10:02
  • The code is supposed to multiply each number with its index, not with the next one. – Starlight Apr 06 '17 at 13:31
  • Yeah, I get it. Doesn’t change the comment. – Ry- Apr 06 '17 at 18:01

0 Answers0