How to sort a list by last character of string

后端 未结 4 845
渐次进展
渐次进展 2021-02-19 13:56

I am trying to write a program that orders a list of strings based on the last character in the item.

[\"Tiger 6\", \"Shark 4\", \"Cyborg 8\"] are how my li

4条回答
  •  日久生厌
    2021-02-19 14:28

    nums = ["Tiger 6", "Shark 4", "Cyborg 8"]
    slist=sorted(nums,key = lambda x:x[-1])
    
    1. here nums is an list
    2. in slist it takes list of arguments from the list nums
    3. here lambda function takes argument from list and return it's last value(e.g. if it a first argument from nums (i.e.''Tiger 6'') and it returns 6.
    4. sorted function return a list based on lambda function.

提交回复
热议问题