qua*_*fly 3 python sorting ignore-case
假设我在python中有以下列表
[ [1,"C"], [2, "D"], [3,"a"], [4,"b"] ]
Run Code Online (Sandbox Code Playgroud)
我想用字母对列表进行排序,这样就可以了
[ [3,"a"], [4,"b"], [1,"C"], [2, "D"] ]
Run Code Online (Sandbox Code Playgroud)
按内在特征排序,我愿意sorted(unsortedlist, key=itemgetter(1)).
通过忽略案例排序,我会这样做sorted(unsortedlist, key=str.lower).
如何按内部元素排序并同时忽略大小写?
它是匿名函数的(罕见)用例之一:
>>> sorted([[1, 'C'], [2, 'D'], [3, 'a'], [4, 'b']], key=lambda x: x[1].lower())
[[3, 'a'], [4, 'b'], [1, 'C'], [2, 'D']]
Run Code Online (Sandbox Code Playgroud)
Lambda通常有点笨拙和unpythonic,但不幸的是,python内置了没有"compose"功能.