class Point:
def init(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '({}, {})'.format(self.x, self.y)
points = [Point(9, 2), Point(1,5), Point(2, 7), Point(3, 8), Point(2, 5)]
sorted_points = sorted(
points,
lambda (x0, y0), (x1, y1): x0 - x1 if x0 != x1 else y0 - y1,
lambda point: (point.x, point.y))
print(', '.join(map(str, sorted_points)))
#预期结果为(1, 5), (2, 5), (2, 7), (3, 8), (9, 2)
init 变成 init
str 变成 str
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
def __str__(self):
return '({},{})'.format(self.x,self.y)
points = [Point(9,2),Point(1,5),Point(2,7),Point(3,8),Point(2,5)]
sorted_points = sorted(points,
lambda (x0,y0),(x1,y1):x0-x1 if x0 != x1 else y0 - y1,
lambda point:(point.x,point.y))
print(','.join(map(str,sorted_points)))
下面的代码在python3下正常运行
#!/usr/bin/env python
# coding=utf-8
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '({}, {})'.format(self.x, self.y)
points = [Point(9, 2), Point(1,5), Point(2, 7), Point(3, 8), Point(2, 5)]
sorted_points = sorted(
points,
key=lambda point: (point.x, point.y)
)
print(', '.join(map(str, sorted_points)))
#预期结果为(1, 5), (2, 5), (2, 7), (3, 8), (9, 2)