3

I have a list of dicts of the structure

list_of_dicts = [  
{'apples': 123, 'bananas': '556685', 'date': some_date},  
{'apples': 99, 'bananas': '556685', 'date': some_date},  
{'apples': 99, 'bananas': '556685', 'date': some_other_date}, 
{'apples': 88, 'bananas': '2345566', 'date': some_other_date}]

plus a few other fields that do not need to be sorted by.

I've already sorted by apples and date, but I am brainfarting on the idea of how to get a list of only the dicts with the most apples per day a lá an SQL query
SELECT max(apples), from TABLE where location in (list of location names) group by date
to get something like

[ {'apples': 123, 'bananas': '556685' 'date': some_date}, {'apples': 99, 'bananas': '556685' 'date': some_other_date}]

I've already tried b = max(temp_top, key = lambda f: (max(f['apples']), f['date'])) but that gives me the dictionary with the most apples over all while I'm trying to get the most apples for each day.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Isaac
  • 625
  • 1
  • 12
  • 30
  • possible duplicate of [How do I sort a list of dictionaries by values of the dictionary in Python?](http://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python) – ivan_pozdeev Mar 10 '15 at 19:24
  • @ivan_pozdeev Not really. I figured that bit out already, partially with the help of that question :) . I edited the the question to try to make it more clear – Isaac Mar 10 '15 at 19:51
  • Break it into more steps. First calculate the `day → total apples` mapping. Then find the day with the greatest number of total apples. Then filter. – Veedrac Mar 11 '15 at 04:28

2 Answers2

1

Going straight ahead, no rocket science:

#group by date
unique_dates={v['date'] for v in data}

#calculate the aggregation function for each group
date_maxapples={d,max(v['apples'] for v in data if v['date']==d) for d in unique_dates}

This may not be the fastest way algorithmically (the list is traversed many times). Yet, it's simple and readable while being not very suboptimal, which is the Python's way of doing things. It might actually be faster than a more sophisticated loop with on-the-fly max calculation (as one would do in C) since most functions used are built-ins (see Faster alternatives to numpy.argmax/argmin which is slow for an example of this paradox).

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
-1

You can try to do this.

import numpy as np
apples = np.array(map(lambda theDict: theDict.get("apples"),list_of_dicts))
maxapples = apples == max(apples)

and then you can access the elements with list_of_dicts[maxapples], where maxapples is a list like [True,False,True...]

Ulugutz
  • 31
  • 2