类型错误:在 Python 实例之间不支持“<”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43477958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: '<' not supported between instances Python
提问by Zaidur
I am solving a problem with genetic algorithm in python 3. I have not completed the full code yet. I test a part of the code whenever I complete it.
我正在解决python 3中的遗传算法问题。我还没有完成完整的代码。每当我完成代码的一部分时,我都会对其进行测试。
At present, I am stuck with an error saying:
目前,我遇到了一个错误,说:
TypeError: '<' not supported between instances of 'part' and 'part'
类型错误:“part”和“part”的实例之间不支持“<”
The interesting thing is, this error does not always show. Sometimes the code runs smoothly and show the desired output, but sometimes it shows this error.
有趣的是,这个错误并不总是显示。有时代码运行流畅并显示所需的输出,但有时会显示此错误。
What is the reason for this?
这是什么原因?
I am attaching the code and the error message.
I am using PyCharm.
我附上了代码和错误信息。
我正在使用 PyCharm。
import random
class part():
def __init__(self, number):
self.number = number
self.machine_sequence = []
def add_volume(self, volume):
self.volume = volume
def add_machine(self, machine_numbers):
self.machine_sequence.append(machine_numbers)
def create_initial_population():
part_family = []
for i in range(8):
part_family.append(part(i))
part_population = []
for i in range(6):
part_population.append(random.sample(part_family, len(part_family)))
for i in part_population:
for j in i:
j.add_volume(random.randrange(100, 200))
return part_population
def fitness(part_family):
sum_of_boundary = []
for i in range(0, 8, 2):
sum_of_boundary.append(sum(j.volume for j in part_family[i:i + 2]))
fitness_value = 0
for i in range(len(sum_of_boundary) - 1):
for j in range(i + 1, len(sum_of_boundary)):
fitness_value = fitness_value + abs(sum_of_boundary[i] - sum_of_boundary[j])
return fitness_value
def sort_population_by_fitness(population):
pre_sorted = [[fitness(x),x] for x in population]
sort = [x[1] for x in sorted(pre_sorted)]
for i in sort:
for j in i:
print(j.volume, end = ' ')
print()
return sort
def evolve(population):
population = sort_population_by_fitness(population)
return population
population = create_initial_population()
population = evolve(population)
the error message:
错误信息:
The Output is (which is randomized every time):
输出是(每次都是随机的):
回答by dhke
Given that pre_sorted
is a list of lists with items [fitness, part]
, this croaks whenever comparing two sublists with the same fitness
.
鉴于这pre_sorted
是一个包含项目的列表列表,[fitness, part]
每当比较两个具有相同fitness
.
Python lists sort lexicographically and are compared element-wise left to right until a mismatching element is found. In your case, the second element (part
) is only accessed if the fitness of two parts is the same.
Python 列表按字典顺序排序,并按元素从左到右进行比较,直到找到不匹配的元素。在您的情况下,part
仅当两个部分的适应度相同时才访问第二个元素 ( )。
[0, part0] < [1, part1]
=> does not comparepart0
andpart1
since the fitness is already different.[0, part0] < [0, part1]
=> doescomparepart0
andpart1
since the fitness is the same.
[0, part0] < [1, part1]
=> 不进行比较part0
,part1
因为适应度已经不同。[0, part0] < [0, part1]
=>确实比较part0
,part1
因为适应度是相同的。
Suggestion 1
建议1
Sort only by fitness: sorted(pre_sorted, key=operator.itemgetter(0))
仅按适合度排序: sorted(pre_sorted, key=operator.itemgetter(0))
Suggestion 2
建议2
Read the documentation for functools.total_ordering
give part
a total order:
阅读文档functools.total_ordering
给part
全序:
@total_ordering
class part():
[...]
def __lt__(self, other):
return self.number < other.number
And yeah, sorting lists of lists seems wrong. The inner elements might better be tuples, so you cannot accidentally modify the contents.
是的,对列表进行排序似乎是错误的。内部元素最好是元组,因此您不会意外修改内容。
回答by fuzzything44
So pre_sorted
is a list with elements of [int, part]
. When you sort this list and have two elements with the same integer value, it then compares the part
values to try to determine which goes first. However, since you have no function for determining if a part is less than a part, it throws that error.
所以,pre_sorted
是的元素的列表[int, part]
。当您对此列表进行排序并有两个具有相同整数值的元素时,它会比较这些part
值以尝试确定哪个先排。但是,由于您没有确定零件是否小于零件的功能,因此会引发该错误。
Try adding a function __lt__(self, other)
to be able to order parts.
尝试添加一个功能__lt__(self, other)
来订购零件。