python sort without lambda expressions

后端 未结 4 1577
小蘑菇
小蘑菇 2021-02-09 06:30

I often do sorts in Python using lambda expressions, and although it works fine, I find it not very readable, and was hoping there might be a better way. Here is a typical use

4条回答
  •  伪装坚强ぢ
    2021-02-09 06:50

    Not elegantly, but:

    [a for (v, a) in sorted((x[a], a) for a in y)]
    

    BTW, you can do this without creating a separate list of indices:

    [i for (v, i) in sorted((v, i) for (i, v) in enumerate(x))]
    

提交回复
热议问题