Skip to main content
 首页 » 编程设计

python之如何在 Python 中正确排序对象列表

2025年02月15日11小虾米

有一个位置类

class Location(object): 
    def __init__(self, x, y): 
        self.x = x 
        self.y = y 
    def move(self, deltaX, deltaY): 
        return Location(self.x + deltaX, self.y + deltaY) 
    def getX(self): 
        return self.x 
    def getY(self): 
        return self.y 
    def dist_from(self, other): 
        xDist = self.x - other.x 
        yDist = self.y - other.y 
        return (xDist**2 + yDist**2)**0.5 
    def __eq__(self, other): 
        return (self.x == other.x and self.y == other.y) 
    def __str__(self): 
        return '<' + str(self.x) + ',' + str(self.y) + '>' 

还有一个 get_cars 方法,我根据以下规范编写该方法以从列表中检索数据(不属于 LOcation 类):

def get_cars(self): 
    """ Returns a list of all cars on the parking. The list should contain  
    the string representation of the Location of a car. The list should  
    be sorted by the x coordinate of the location. """ 
 
 
    result = '' 
    for i in sorted(self.car_loc_list, key=lambda Location: Location.x): 
        if self.car_loc_list[0] == i: 
            result += '\'' + str(i) + '\'' 
        else:     
            result += ', ' + '\'' + str(i) + '\'' 
 
    return '[' + result + ']' 

self.car_loc_list 只是一个包含 Location 类 对象的列表,它们包含一些坐标 (x,y)(未排序):

for i in c.car_loc_list: 
    print(i) 
 
<0,0> 
<1,5> 
<7,2> 
<2,1> 
<1,7> 
<4,3> 

在线评分器以两种方式检查我的代码:

  1. print(c.get_cars()) - 确定
  2. print(sorted(c.get_cars())) - 不行

当我按照第一种方式时:

print(c.get_cars()) 

它打印出按 X 坐标(第 1 位)排序的下一个结果:

print(c.get_cars()) 
Out[539]: "['<0,0>', '<1,5>', '<1,7>', '<2,1>', '<4,3>', '<7,2>']" 

这也是我(和评分者)期望得到的结果。

当我执行 print(sorted(c.get_cars)) 时,我得到:

print(sorted(c.get_cars())) 
[' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', '0', '0', '1', '1', '1', '2', '2', '3', '4', '5', '7', '7', '<', '<', '<', '<', '<', '<', '>', '>', '>', '>', '>', '>', '[', ']'] 

我卡在了这个地方。我也明白它以某种方式再次将我的输出转换为字符串,这就是为什么我得到这样的结果。是否有任何想法如何实现,以便两种解决方案根据上述规范给出相同的结果,例如 ['<0,0>', '<1,5>', '<1,7>', '<2,1>', '<4,3>', '<7,2>'] ?

UPD 下一句好像没听懂:

The list should contain the string representation of the Location of a car.

请您参考如下方法:

你的方法的描述很清楚你哪里出错了:

""" Returns a list of all cars on the parking. The list should contain  
the string representation of the Location of a car. The list should  
be sorted by the x coordinate of the location. """ 

您没有返回列表。您正在返回一个字符串,其内容看起来像一个列表。这不是一回事。

返回一个列表:

def get_cars(self): 
    """ Returns a list of all cars on the parking. The list should contain  
    the string representation of the Location of a car. The list should  
    be sorted by the x coordinate of the location. """ 
 
    result = [] 
    for loc in sorted(self.car_loc_list, key=lambda loc: loc.x): 
        result.append(str(loc)) 
    return result 

或更简单地使用列表理解:

def get_cars(self): 
    """ Returns a list of all cars on the parking. The list should contain  
    the string representation of the Location of a car. The list should  
    be sorted by the x coordinate of the location. """ 
 
    return [str(loc) for loc in sorted(self.car_loc_list, key=lambda loc: loc.x)]