我正在学习python,但我不知道使用许多属性对对象列表进行排序的最佳方法是什么。现在我有这个
class Example:
def __init__(self, a,b,c):
self.a = a
self.b = b
self.c = c
List = [Example(3,1,5), Example(2,1,2), Example(2,2,2), Example(1,4,1),Example(1,4,5), Example(1,4,2)]
我不知道如何排序。Python中是否有任何工具可以帮助您解决此问题,或者需要编写一些自定义函数?
您可以按以下多个项目进行排序:
List.sort(key=lambda e: [e.a, e.b, e.c])
# or
List.sort(key=operator.attrgetter('a', 'b', 'c'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句