在python中获取列表中索引的最大值、最小值和平均值

2024-09-28 21:35:08 发布

您现在位置: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]

我不知道如何获取和返回这些值。希望你能帮助我。非常感谢


Tags: theinselfforindexifcounterattribute