• <small id='IO3KG'></small><noframes id='IO3KG'>

  • <legend id='IO3KG'><style id='IO3KG'><dir id='IO3KG'><q id='IO3KG'></q></dir></style></legend>
  • <i id='IO3KG'><tr id='IO3KG'><dt id='IO3KG'><q id='IO3KG'><span id='IO3KG'><b id='IO3KG'><form id='IO3KG'><ins id='IO3KG'></ins><ul id='IO3KG'></ul><sub id='IO3KG'></sub></form><legend id='IO3KG'></legend><bdo id='IO3KG'><pre id='IO3KG'><center id='IO3KG'></center></pre></bdo></b><th id='IO3KG'></th></span></q></dt></tr></i><div id='IO3KG'><tfoot id='IO3KG'></tfoot><dl id='IO3KG'><fieldset id='IO3KG'></fieldset></dl></div>

          <bdo id='IO3KG'></bdo><ul id='IO3KG'></ul>
      1. <tfoot id='IO3KG'></tfoot>

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

        时间:2023-09-13
          <tbody id='wAXVr'></tbody>

          <legend id='wAXVr'><style id='wAXVr'><dir id='wAXVr'><q id='wAXVr'></q></dir></style></legend>
            • <tfoot id='wAXVr'></tfoot>
              • <bdo id='wAXVr'></bdo><ul id='wAXVr'></ul>

                <i id='wAXVr'><tr id='wAXVr'><dt id='wAXVr'><q id='wAXVr'><span id='wAXVr'><b id='wAXVr'><form id='wAXVr'><ins id='wAXVr'></ins><ul id='wAXVr'></ul><sub id='wAXVr'></sub></form><legend id='wAXVr'></legend><bdo id='wAXVr'><pre id='wAXVr'><center id='wAXVr'></center></pre></bdo></b><th id='wAXVr'></th></span></q></dt></tr></i><div id='wAXVr'><tfoot id='wAXVr'></tfoot><dl id='wAXVr'><fieldset id='wAXVr'></fieldset></dl></div>
                1. <small id='wAXVr'></small><noframes id='wAXVr'>

                2. 本文介绍了在对象列表中获取具有 max 属性值的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我到目前为止编写的代码,程序的重点是从一个文件中读取 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
                  

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

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

                4. 如果您发现需要通过相同的属性一次又一次地在各个方向上对 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.

                  这篇关于在对象列表中获取具有 max 属性值的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:向python对象添加属性 下一篇:Python matplotlib 减小颜色条标签的大小

                  相关文章

                  最新文章

                    <bdo id='0hvWK'></bdo><ul id='0hvWK'></ul>
                  1. <legend id='0hvWK'><style id='0hvWK'><dir id='0hvWK'><q id='0hvWK'></q></dir></style></legend>
                    <i id='0hvWK'><tr id='0hvWK'><dt id='0hvWK'><q id='0hvWK'><span id='0hvWK'><b id='0hvWK'><form id='0hvWK'><ins id='0hvWK'></ins><ul id='0hvWK'></ul><sub id='0hvWK'></sub></form><legend id='0hvWK'></legend><bdo id='0hvWK'><pre id='0hvWK'><center id='0hvWK'></center></pre></bdo></b><th id='0hvWK'></th></span></q></dt></tr></i><div id='0hvWK'><tfoot id='0hvWK'></tfoot><dl id='0hvWK'><fieldset id='0hvWK'></fieldset></dl></div>

                  2. <small id='0hvWK'></small><noframes id='0hvWK'>

                    <tfoot id='0hvWK'></tfoot>