我们知道这两种方法都适用于 sorted():
sorted(['second', 'first', 'third'])
sorted([('first','second'), ('second', 'first'), ('first', 'third')])
通过对第二个进行排序,元组按字典顺序进行比较;比较第一个项目;如果它们相同,则比较第二个项目,依此类推。
但是如何在所有单独的字符串(或其他任何字符串)上应用一个 key 函数来进行排序,它适用于容器并在第二种情况下递归地工作?假设 func 将“first”转换为 3,将“second”转换为 1,将“third”转换为 2。我想要这个结果:
['second', 'third', 'first']
[('second', 'first'), ('first','second'), ('first', 'third')]
我将此函数用作键,但我不喜欢在其中进行类型检查,因为它仅将 func 应用于不是通用解决方案的字符串:
def recursively_apply_func_on_strings(target, func,
fargs=(), fkwargs={}):
if isinstance(target, str):
return func(target, *fargs, **fkwargs)
result, f = [], recursively_apply_func_on_strings
for elem in target:
result.append(f(elem, func, fargs, fkwargs))
return tuple(result)
sorted(sequence, key=lambda x: recursively_apply_string_func(x, func))
有没有更清洁的方法来做到这一点?