magnoliafork
magnoliafork

Reputation: 95

Finding max value of an attribute of objects in a dictionary

I have a collection of objects stored in a dictionary, and I am wanting to retrieve the maximum value of a particular attribute. The objects are cookies (think chocolate chip), and each cookie has its own temperature, heat capacity, batch number, etc. The batch number describes which set of cookies it exited the oven with.

class Cookie:
def __init__(self, density, specificheat, batch):
    self.rho = density
    self.cp = specificheat
    self.batch = batch
    self.volume = 1.2e-5        # m^3
    self.surfarea = 4.9e-3      # m^2
    ...

I want to find the maximum batch number in the dictionary. I know this would be simple if I were using a list, I could just use operator.attrgetter('batch') to sort the list and get the value. However, using a dictionary, this line does not work:

sorted(cookies, key=operator.attrgetter('batch'))

I could set up a for loop and go through every single object in the dictionary, but I feel there is a better way to do this. I want to avoid switching to a list because I'm running an optimization sorting the cookies into boxes, and it is faster to retrieve data from a dictionary than from a list when you have to worry about 2000 cookies. (I timed it using timeit()). I've looked for similar questions on Stackoverflow, but most of them seem to be in javascript. The Python questions I've found are for dictionaries that don't store objects like mine. Any help would be greatly appreciated!

Upvotes: 2

Views: 1738

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You could just use max on the values:

mx_batch =  max(cookies.values(), key=operator.attrgetter('batch')).batch

Or just use a class attribute to keep the max:

class Cookie:
    mx  = 0
    def __init__(self, density, specificheat, batch):
        self.rho = density
        self.cp = specificheat
        self.batch = batch
        self.volume = 1.2e-5        # m^3
        self.surfarea = 4.9e-3      # m^2
        if batch > Cookie.mx:
            Cookie.mx = batch

Then:

In [9]: c1 = Cookie(1, 120, 1)

In [10]: c2 = Cookie(2, 120, 4)

In [11]: c3 = Cookie(1, 120, 2)

In [12]: c4 = Cookie(1, 120, 1)

In [13]: print(Cookie.mx)
4

Upvotes: 1

qfwfq
qfwfq

Reputation: 2516

Here is one way to do it

# where objs is your dictionary of cookies
max(map(lambda i: objs[i].batch, objs))

Upvotes: 0

Related Questions