需要帮助按键对对象列表进行排序
我无法让此代码使用 .sort() 或sorted() 对对象列表进行排序。我在这里缺少什么?
PS 如果有人有任何建议,我的解决方案.distance() 方法也可以使用一些整容手术。
谢谢!
import random
import math
POPULATION_SIZE = 100
data = [[1, 565.0, 575.0],
[2, 25.0, 185.0],
[3, 345.0, 750.0],
[4, 945.0, 685.0],
[5, 845.0, 655.0],
[6, 880.0, 660.0],
[7, 25.0, 230.0],
[8, 525.0, 1000.0],
[9, 580.0, 1175.0],
[10, 650.0, 1130.0]
]
class Solution():
def __init__(self):
self.dna = []
self.randomize()
def randomize(self):
temp = data[:]
while len(temp) > 0:
self.dna.append( temp.pop( random.randint( 0,len(temp)-1 ) ) )
def distance(self):
total = 0
#There has to be a better way to access two adjacent elements.
for i, points in enumerate(self.dna):
if i < (len(self.dna)-1):
total += math.sqrt( (points[1]-self.dna[i+1][1])**2 + (points[2]-self.dna[i+1][2])**2 )
else:
total += math.sqrt( (points[1]-self.dna[0][1])**2 + (points[2]-self.dna[0][2])**2 )
return int(total)
class Population():
def __init__(self):
self.solutions = []
self.generation = 0
#Populate with solutions
self.solutions = [Solution() for i in range(POPULATION_SIZE)]
def __str__(self):
result = ''
#This is the part that is not returning sorted results. I tried sorted() too.
self.solutions.sort(key=lambda solution: solution.distance, reverse=True)
for solution in self.solutions:
result += 'ID: %s - Distance: %s\n' % ( id(solution), solution.distance() )
return result
if __name__ == '__main__':
p = Population()
print p
I am unable to get this code to sort a list of objects using either .sort() or sorted(). What am I missing here?
P.S. My solution.distance() method could use some cosmetic surgery too if anyone has any suggestions.
Thanks!
import random
import math
POPULATION_SIZE = 100
data = [[1, 565.0, 575.0],
[2, 25.0, 185.0],
[3, 345.0, 750.0],
[4, 945.0, 685.0],
[5, 845.0, 655.0],
[6, 880.0, 660.0],
[7, 25.0, 230.0],
[8, 525.0, 1000.0],
[9, 580.0, 1175.0],
[10, 650.0, 1130.0]
]
class Solution():
def __init__(self):
self.dna = []
self.randomize()
def randomize(self):
temp = data[:]
while len(temp) > 0:
self.dna.append( temp.pop( random.randint( 0,len(temp)-1 ) ) )
def distance(self):
total = 0
#There has to be a better way to access two adjacent elements.
for i, points in enumerate(self.dna):
if i < (len(self.dna)-1):
total += math.sqrt( (points[1]-self.dna[i+1][1])**2 + (points[2]-self.dna[i+1][2])**2 )
else:
total += math.sqrt( (points[1]-self.dna[0][1])**2 + (points[2]-self.dna[0][2])**2 )
return int(total)
class Population():
def __init__(self):
self.solutions = []
self.generation = 0
#Populate with solutions
self.solutions = [Solution() for i in range(POPULATION_SIZE)]
def __str__(self):
result = ''
#This is the part that is not returning sorted results. I tried sorted() too.
self.solutions.sort(key=lambda solution: solution.distance, reverse=True)
for solution in self.solutions:
result += 'ID: %s - Distance: %s\n' % ( id(solution), solution.distance() )
return result
if __name__ == '__main__':
p = Population()
print p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
为
(调用该函数需要括号。)
或者,您可以将
distance
方法设为属性:在这种情况下,更改所有出现的
solution.distance()
到solution.distance
。我认为这个替代解决方案更好一点,因为每次您想谈论距离时,它都会消除两个混乱的字符(括号)。附言。
key=lambda 解决方案:solution.distance
为self.solutions
中的每个solution
返回绑定方法solution.distance
>。由于相同的对象作为每个解决方案
的键返回,因此没有发生所需的排序。Change
to
(The parentheses are needed to call the function.)
Alternatively, you could make the
distance
method a property:In this case, change all occurances of
solution.distance()
tosolution.distance
. I think this alternate solution is a little bit nicer, since it removes two characters of clutter (the parens) every time you wish to talk about the distance.PS.
key=lambda solution: solution.distance
was returning the bound methodsolution.distance
for eachsolution
inself.solutions
. Since the same object was being returned as the key for eachsolution
, no desired ordering occurred.这是使用函数式编程技术清理类的尝试:
这未经测试,但应该接近工作。另外,建议使用类而不是数据的三元素列表。它将使您的代码更清晰易读:
Here's an attempt at cleaning up your class, using functional programming techniques:
This is untested, but should be close to working. Also, a suggestion would be to use a class instead of a 3-element list for data. It will make your code much clearer to read:
这是在
distance()
中编写循环的更好方法:放置以下函数定义,取自 itertools 文档,在你的代码中:然后你可以编写
distance
来利用 Python 高效的迭代器操作例程,如下所示:这应该是相当有效的,即使
dna
数组非常长。Here's a better way to write the loop in
distance()
: put the following function definition, taken from the itertools documentation, in your code:Then you can write
distance
to take advantage of Python's efficient iterator manipulation routines, like so:This should be reasonably efficient even if the
dna
array is very long.