课程
/后端开发
/Python
/python进阶
练习中L2的sorted()跟python3中有些区别
在python3中如何实现L2
sorted(literals, key, reverse)
2020-03-11
源自:python进阶 4-3
正在回答
在python3.x更好实现哦,如下:
class Person(object): pass p1 = Person() p1.name = 'Bart' p2 = Person() p2.name = 'Adam' p3 = Person() p3.name = 'Lisa' L1 = [p1, p2, p3] L2 = sorted(L1, key = lambda x: x.name) print L2[0].name print L2[1].name print L2[2].name
class Person(object):
pass
p1 = Person()
p1.name = ('Bart')
p2 = Person()
p2.name = ('Adam')
p3 = Person()
p3.name = ('Lisa')
L1 = [p1, p2, p3]
L2 = sorted(L1, lambda x, y : cmp(x.name, y.name))
print L2[0].name
print L2[1].name
print L2[2].name
举报
学习函数式、模块和面向对象编程,掌握Python高级程序设计