我尝试在这里编写代码并获得正确的 Fitness_point,但在循环之后,结果一直显示为 0。它首先显示 StopIteration,所以我在那里写了 try 和 except,但结果仍然是错误的。我是这里的新手,我应该怎么办?请帮忙..
population={'list_1':[0,2,3,4],'list_2':[2,0,4,1],'list_3':[1,6,0,8],'list_4':[3,5,6,0]}
item = [3,4,2,1]
def get_fitness(items):
g= iter(items)
temp=[]
fitness_point=0
try:
for i in items:
if i != items[3]:
temp=population['list_{}'.format(i)]
fitness_point+=temp[g.__next__()-1]
print(fitness_point)
except GeneratorExit:
return 'eror'
return fitness_point
print(get_fitness(item))