Get the object with the max attribute#39;s value in a list of objects(在对象列表中获取具有 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
只将属性传递给 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.
这篇关于在对象列表中获取具有 max 属性值的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在对象列表中获取具有 max 属性值的对象


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01