Sort a list numerically in Python

前端 未结 4 653
野性不改
野性不改 2021-01-23 03:27

So I have this list, we\'ll call it listA. I\'m trying to get the [3] item in each list e.g. [\'5.01\',\'5.88\',\'2.10\',\'9.45\',\'17.58\',\'2.76\'] in sorted orde

4条回答
  •  感动是毒
    2021-01-23 04:09

    An anonymous lambda function is not necessary for this task. You can use operator.itemgetter, which may be more intuitive:

    from operator import itemgetter
    
    res1 = sorted(map(itemgetter(3), listA), reverse=True, key=float)
    
    ['17.58', '9.45', '5.88', '5.01', '2.76', '2.10']
    

提交回复
热议问题