Search code examples
pythonmaxaveragemingenetic-algorithm

Get maximum, minimum and average value of an index in a list in python


My code is on genetic algorithm. My problem is: I want to return the best individual, worst and average. All I want to do with the index of a list. Example: Best Value = High rate more. Worst value = the smallest index. I do not know how. This is my code:

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]

I do not know how to take and return these values​​. Hope you can help me. Thank you very much


Solution

  • You have this error because you're calling the method without passing any list (a population in this case).