【问题标题】:Get maximum, minimum and average value of an index in a list in python获取python列表中索引的最大值、最小值和平均值
【发布时间】:2014-07-09 16:07:54
【问题描述】:

我的代码是关于遗传算法的。我的问题是: 我想返回最好的个人,最差的和平均的。我想做的只是列表的索引。示例:最佳价值 = 高比率更多。最差值 = 最小索引。我不知道怎么。这是我的代码:

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]

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

【问题讨论】:

  • 我在回答您关于平均部分的问题时遇到了问题:您希望找到平均指数在列表中出现的位置?如果是这样,请注意:您的平均值可能不存在,具体取决于列表中的值(例如,平均值可能是浮点数,列表中的数字可能只是整数)。
  • 也许我解释得不好。我希望在每次迭代中实现以下目标: - 评估人群中最好的个体。 - 评估人口中最差的个体。 - 人口的平均分数。这条剩余线:index,value = max(enumerate(list),key=operator.itemgetter(1))
  • 我想你只需要写一个基本的算法。我不确定您想要的最大值、最小值或平均值的确切数据,所以我犹豫要不要写答案。

标签: python max average min genetic-algorithm


【解决方案1】:

你有这个错误是因为你在没有传递任何列表的情况下调用方法(在这种情况下是人口)。

【讨论】:

    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2018-07-01
    相关资源
    最近更新 更多