在Python中通过子字符串对字符串进行排序

use*_*001 3 python sorting string substring

我在python中有一个字符串列表,如下所示:

  • 名称编号4位数字

如何按最后一个数字对其进行排序?

Sam*_*uns 15

像那样:

sorted(your_list, lambda x: int(x.split()[-1]))
Run Code Online (Sandbox Code Playgroud)

  • 第二个参数(可选)*必须*指定为关键字参数(https://docs.python.org/3/library/functions.html#sorted)。所以 `sorted(your_list, key=lambda x: int(x.split()[-1]))` (5认同)

eum*_*iro 13

my_list = ['abc 12 34 3333',
           'def 21 43 2222',
           'fgh 21 43 1111']

my_list.sort(key=lambda x:int(x.split()[-1]))
Run Code Online (Sandbox Code Playgroud)

my_list 就是现在: ['fgh 21 43 1111', 'def 21 43 2222', 'abc 12 34 3333']