Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the object with the max attribute's value in a list of objects

Tags:

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)  

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

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!

like image 964
Filip Avatar asked Aug 01 '13 21:08

Filip


People also ask

How will you get the max value item of a list in Python?

Python max() Function The max() function returns the item with the highest value, or the item with the highest value in an iterable.

How do you find the index of a max value in a list?

Use for loop to find out the index of the maximum value in a list. Use the max() and list. index() functions to find out the index of the maximum value in a list. Use the enumerate() function to find out the index of the maximum value in a list.

Can we use MAX function in list?

Python list max() function returns the maximum value present in the list.


1 Answers

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

Use operator.attrgetter() to get that value:

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 
  • Pass just the attributes instead to max() with a generator expression:

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

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.

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 

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.

like image 58
Martijn Pieters Avatar answered Oct 04 '22 20:10

Martijn Pieters