Python 使用itemgetter代替lambda

Python 使用itemgetter代替lambda,python,python-3.x,Python,Python 3.x,是否可以使用itemgetter运算符将以下排序放入 res = sorted(res, key = lambda x: (x['operation'], x['path'])) 以前我使用过res.sort(key=itemgetter(“path”),但我在计算如何使用多种排序时遇到了困难。您可以: from operator import itemgetter res = [{"operation": 1, "path": 2}, {"operation": 1, "path": 1}

是否可以使用
itemgetter
运算符将以下排序放入

res = sorted(res, key = lambda x: (x['operation'], x['path']))
以前我使用过res.sort(key=itemgetter(“path”),但我在计算如何使用多种排序时遇到了困难。

您可以:

from operator import itemgetter

res = [{"operation": 1, "path": 2}, {"operation": 1, "path": 1}]

res = sorted(res, key=itemgetter("operation", "path"))

print(res)
输出

[{'operation': 1, 'path': 1}, {'operation': 1, 'path': 2}]

key=itemgetter(“操作”、“路径”)
@DanielMesejo--谢谢,你为什么不把它写进一个答案,我会接受它。恕我直言,但你不能从…?