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

时间:2017-08-01 19:04:50

标签: python python-3.x class sorting

位置等级

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 只是一个包含位置类对象的列表,它们包含一些坐标(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())) - 不行
  3. 当我按照第一种方式行事时:

    print(c.get_cars())
    

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

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

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

    当我打印(已排序(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 似乎我不明白下一句话:

      

    列表应包含       汽车位置的字符串表示。

1 个答案:

答案 0 :(得分:1)

在您出错的地方,您的方法说明非常明确:

""" 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)]