[英]Get maximum, minimum and average value of an index in a list in python
我的代码是关于遗传算法的。 我的问题是:我想返回最好的个人,最差的和平均的。 我要做的只是列表索引。 示例:最佳价值=高利率。 最差值=最小索引。 我不知道怎么。 这是我的代码:
def selecciona_mejor_peor_medio(self, population):
list = []
cont = 0
arff = self.arff_reader_class
attributes_list = []
attributes_counter = []
for element in list:
if element[len(element)-1] in arff.consequences:
temporal_rule = element
# We take the first attribute
for attribute in element:
# Seek other sets that start with the same item and group them into lists.
# Subsequently removed from the initial list to avoid repeating values
for rule_list in self.population_my_method:
for rule in rule_list:
cont_attribute = 0
for rule_attribute in rule:
if rule_attribute == attribute:
cont_attribute += 1
if cont_attribute == len(element):
cont += 1
attributes_counter.append(cont)
# Find the index of the highest value, medium and low
peor = attributes_counter.index(min(attributes_counter))
mejor = attributes_counter.index(max(attributes_counter))
index, value = max(enumerate(list), key=operator.itemgetter(1))
# With the rates you get the values at the population
if mejor > list[cont]:
return mejor
elif peor < list[cont]:
return peor
else:
return list[cont]
我不知道如何获取并返回这些值。 希望您能够帮助我。 非常感谢你
出现此错误是因为您在调用方法时未传递任何列表(在这种情况下为填充)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.