Python - 成员函数的 argmin / argmax

Python - argmin / argmax for member function

我想知道是否有一种 "pythonic" 方法可以将数学 argmin/argmax 用于成员函数 而无需 使用像 [=22 这样的库=]numpy.

我有一个 class 和一个成员函数,其中 return 是一个整数。 我实例化了这个 class 的几个对象。 我想知道哪个对象对此方法具有较低的 return 值。

请在下面找到我的源代码。 我想改进的部分就在标签 我想改进的代码 之后。这段代码工作得很好,但我很确定有更好的方法来做同样的事情。

#!/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 !

以更 pythonic 的方式编写此代码的一个好方法是使用 min 函数,实际上可以像 argmin 函数得益于其参数 key.

如果我们替换标签我想改进的代码我想改进的代码结尾之间的代码通过 :

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()