python 根据内部元素并忽略大小写对列表列表进行排序

python sort list of lists based on inner element AND ignore case

假设我在 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.

要么是 lambda:

sorted(unsortedlist, key=lambda x: x[1].lower())

或常规函数:

def my_key(x):
    return x[1].lower()

sorted(unsortedlist, key=my_key)