在对象列表中获取具有 max 属性值的对象

2022-01-13 00:00:00 python python-3.x class attributes

问题描述

这是我到目前为止编写的代码,程序的重点是从一个文件中读取 20 个人,然后分配他们的属性,然后根据用户给出的输入规范化他们的值.

This is the code I written so far, and the point with the program is to read 20 people from a file and then assign them their attributes, then normalise their values from a input given by the user.

class One:
    def __init__(self):
        self.attrOne = ()
        self.attrTwo = ()
        self.attrThree = ()
        self.attrFour = ()
        self.attrFive= ()
        self.attrSix = ()
        self.attrSeven = ()
        self.attrEight = ()
        self.attrNine = ()


class Two:

    def __init__(self):
        self.allPersons = []

   def importFromList(self, filename): 
       file= open(filename, "rU")
       for line in file:
           partOfList = line.split()                        
           x = Partner() 
           x.attrOne = partOfList[0]
           x.attrTwo = partOfList[1]
           x.attrThree = partOfList[2]
           x.attrFour = partOfList[3]
           x.attrFive = partOfList[4]
           x.attrSix = partOfList[5]
           x.attrSeven = partOfList[6]
           x.attrEight= partOfList[7]
           x.attrNine = partOfList[8]
           self.addPerson(x)
        file.close()

def addPerson(self, x):
    self.allPersons.append(x) 

我想知道的是如何遍历放在 allPersons 列表中的人员的属性,然后将它们相互比较以找出最大值.这是我迄今为止尝试过的,但我无法让它工作

What I wonder is how to loop through the attributes of the persons that is placed in allPersons list and then compare them against eachother to find out the max value. This is what I tried so far, but I can't get it to work

def getMaxValue(self): 
    o = One()
    for eachPartner in self.allPartners:
        maxValuesAttrOne = max(O.attrOne))

所有帮助将不胜感激,我愿意接受新的解决方案,而且我认为 importFromList 方法不是最有效的方法,所以如果您有任何异议,我愿意倾听和学习!

All help will be appreciated, and I'm open for new solutions, also I imagine the importFromList method is not the most effective one, so if you got any objections I'm willing to listen and learn!


解决方案

max() 采用 key 参数,该函数在传递其中一个对象时返回比较它们的值.

max() takes a key parameter, a function that when passed one of the objects returns the value by which to compare them.

使用 operator.attrgetter() 获取该值:

from operator import attrgetter

max(self.allPartners, key=attrgetter('attrOne'))

这将返回该属性为最大值的匹配对象.如果您只想存储该最大值本身,您有两种选择:

This returns the matching object for which that attribute is the maximum. If you wanted to store just that maximum value itself, you have two options:

  • 从返回的对象中获取属性:

  • Take the attribute from the returned object:

max(self.allPartners, key=attrgetter('attrOne')).attrOne

  • 只将属性传递给 max() 使用生成器表达式:

    max(p.attrOne for p in self.allPartners)
    

  • 如果您发现需要通过相同的属性一次又一次地在各个方向上对 One 类进行排序(以查找最小值、最大值、对它们进行排序等),您可能需要你的班级可订购.

    If you find that you need to order the One classes in various directions by the same attribute again and again (to find the minimum, maximum, sort them, etc.) you may want to make your class orderable as well.

    为此,您需要实现 一些 基本的自定义钩子 Python 会寻找.通过一些额外的技巧,您可以只使用小于和等于操作,并使用 funtools.total_ordering 类装饰器:

    To do that, you'll need to implement some of the basic customization hooks Python will look for. With some extra trickery, you can get away with just the lower-than and equals operations, and by using the funtools.total_ordering class decorator:

    from functools import total_ordering
    
    @total_ordering
    class One:
        # ...
        def __lt__(self, other):
            if not isinstance(other, type(self)): return NotImplemented
            return self.attrOne < other.attrOne
    
        def __eq__(self, other):
            if not isinstance(other, type(self)): return NotImplemented
            return self.attrOne == other.attrOne
    

    现在您的 One 类是可排序的,完全基于 attrOne;对于 max() 函数,这意味着您可以完全删除 key 参数.

    Now your One class is orderable, entirely on the basis of attrOne; for the max() function, that means you can drop the key parameter altogether.

    相关文章