python sort without lambda expressions -


I often do some type of Python using lambda expressions, and although it works fine, It does not seem to be readable, and was hoping to be a better way for me here is a specific use case.

I have a list of numbers like x = [12, 101, 4, 56, ...]

I have an index There is a separate list: y = boundary (lane (x))

I want to sort y x Value, and I do this:

  y.sort (key = lambda a: x [a])  

without using lambda Is there a good way to do this?

You can use __getitem__ to list x Treats like lambda and it will be very fast because it is implemented as a c function instead of python function:

  & gt; & Gt; & Gt; X = [12, 101, 4, 56]> gt; & Gt; & Gt; Y = boundary (lane (x))> gt; & Gt; & Gt; Sorted (y, key = x .__ getitem__) [2, 0, 3, 1]  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -