假设我在 python 中有以下列表
[ [1,"C"], [2, "D"], [3,"a"], [4,"b"] ]
我想按字母对列表进行排序,这样会是
[ [3,"a"], [4,"b"], [1,"C"], [2, "D"] ]
要按内部字符排序,我会执行 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']]
Lambda 通常有点笨重且不符合 Python 规范,但是 unfortunately, there is no "compose" function built-in to python .
关于python 根据内部元素和忽略大小写对列表列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156307/