将python2转换成python3代码

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)

3个回答

下面的代码在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)

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)))
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐