Python - 成员函数的argmin / argmax

时间:2016-06-05 23:33:50

标签: python argmax

我想知道是否有一种“pythonic”方式使用数学argmin / argmax为成员函数使用使用像 numpy 这样的库。

我有一个带有成员函数的类,里面返回一个整数。 我实例化了这个类的几个对象。 我想知道哪个对象的返回值较低。

请在下面找到我的源代码。 我想要改进的部分就是在代码之后我想改进。这段代码运行得很好,但我很确定有更好的方法可以做同样的事情。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""argmin example"""


class People(object):
    """People class"""
    ret_ages = {"Half": 60, "AlmostFull": 65, "Full": 71}  # years

    def __init__(self, name, age, ret_mode):
        super(People, self).__init__()

        if ret_mode not in self.ret_ages.keys():
            raise KeyError(ret_mode + " not in " + str(self.ret_ages.keys()))

        self.name = name
        self.age = age
        self.ret_mode = ret_mode

    def get_remaining_years(self):
        """
                Return how many years People have still to work before earning
                <rate> retirement.
                <rate> could be "Half", "Middle" or "Full".
        """
        try:
            return self.ret_ages[self.ret_mode] - self.age
        except KeyError:
            raise KeyError("rate has to be in " + str(self.ret_ages.keys()))


def main():
    """Main function"""
    people_list = [
        People("Juliette", 35, "Full"),
        People("Coralie", 26, "Half"),
        People("Laura", 27, "AlmostFull")
    ]

    # Debugging print
    for people in people_list:
        print people.name, "has still to work",\
            people.get_remaining_years(), "years."
    print
    # End of debugging print

    ############################
    # Code I'd like to improve #
    ############################

    people_closer_to_ret = people_list[0]
    minimum_remainining_years = people_closer_to_ret.get_remaining_years()

    for people in people_list:
        if people.get_remaining_years() < minimum_remainining_years:
            people_closer_to_ret = people
            minimum_remainining_years = people.get_remaining_years()

            minimum_remainining_years = people.get_remaining_years()

    ###################################
    # End of code I'd like to improve #
    ###################################

    print people_closer_to_ret.name, "will be retired soon !"


if __name__ == '__main__':
    main()

以下是此脚本的输出:

Juliette has still to work 36 years.
Coralie has still to work 34 years.
Laura has still to work 38 years.

Coralie will be retired soon !

1 个答案:

答案 0 :(得分:1)

以更多 pythonic 方式编写此代码的好方法是使用 min 函数,实际上可以像 argmin 函数一样使用感谢其参数

如果我们替换代码代码之间的代码我想要改进代码结束我希望通过以下方式改进

people_closer_to_ret = min(people_list,
                           key=lambda people: people.get_remaining_years()),

它完美无缺。 key 参数对于告诉 min 函数必须最小化哪个条件非常有用。

所以您的完整代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""argmin example"""


class People(object):
    """People class"""
    ret_ages = {"Half": 60, "AlmostFull": 65, "Full": 71}  # years

    def __init__(self, name, age, ret_mode):
        super(People, self).__init__()

        if ret_mode not in self.ret_ages.keys():
            raise KeyError(ret_mode + " not in " + str(self.ret_ages.keys()))

        self.name = name
        self.age = age
        self.ret_mode = ret_mode

    def get_remaining_years(self):
        """
                Return how many years People have still to work before earning
                <rate> retirement.
                <rate> could be "Half", "Middle" or "Full".
        """
        try:
            return self.ret_ages[self.ret_mode] - self.age
        except KeyError:
            raise KeyError("rate has to be in " + str(self.ret_ages.keys()))


def main():
    """Main function"""
    people_list = [
        People("Juliette", 35, "Full"),
        People("Coralie", 26, "Half"),
        People("Laura", 27, "AlmostFull")
    ]

    # Debugging print
    for people in people_list:
        print people.name, "has still to work",\
            people.get_remaining_years(), "years."
    print
    # End of debugging print

    people_closer_to_ret = min(people_list,
                               key=lambda people: people.get_remaining_years())

    print people_closer_to_ret.name, "will be retired soon !"


if __name__ == '__main__':
    main()