我是靠谱客的博主 会撒娇冬瓜,这篇文章主要介绍python sorted 对对象排序,现在分享给大家,希望可以做个参考。

方法一:

class Myobj:
def __init__(self,id,score):
self.id=id
self.score=score
if __name__ == '__main__':
array=[Myobj(1,5),Myobj(2,2),Myobj(3,0)]
array=sorted(array,key=lambda Myobj:Myobj.score)
for obj in array:
print(obj.id,obj.score)

方法二:

class Myobj:
def __init__(self,id,score):
self.id=id
self.score=score
def __lt__(self, other):
return self.score < other.score
if __name__ == '__main__':
array=[Myobj(1,5),Myobj(2,2),Myobj(3,0)]
array=sorted(array)
for obj in array:
print(obj.id,obj.score)

最后

以上就是会撒娇冬瓜最近收集整理的关于python sorted 对对象排序的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部