On 03/20/2013 09:14 PM, Matthew Johnson wrote:
I recently asked a question on SO: http://stackoverflow.com/questions/15180767/subset-list-based-on-value-of-dictionary-element

and got very confused when trying to generalise the answer.

The problem is as follows:

say i have this list, and would like to get the value at each date that has the largest value for realtime_start (date) value.

obs = [{'date': '2012-10-01',
'realtime_end': '2013-02-18',
'realtime_start': '2012-11-15',
'value': '231.751'},
 {'date': '2012-10-01',
'realtime_end': '9999-12-31',
'realtime_start': '2012-12-19',
'value': '231.623'},
 {'date': '2012-11-01',
'realtime_end': '2013-02-18',
'realtime_start': '2012-12-14',
'value': '231.025'},
 {'date': '2012-11-01',
'realtime_end': '9999-12-31',
'realtime_start': '2013-01-19',
'value': '231.071'},
 {'date': '2012-12-01',
'realtime_end': '2013-02-18',
'realtime_start': '2013-01-16',
'value': '230.979'},
 {'date': '2012-12-01',
'realtime_end': '9999-12-31',
'realtime_start': '2013-02-19',
'value': '231.137'},
 {'date': '2012-12-01',
'realtime_end': '9999-12-31',
'realtime_start': '2013-03-19',
'value': '231.197'},
 {'date': '2013-01-01',
'realtime_end': '9999-12-31',
'realtime_start': '2013-02-21',
'value': '231.198'},
 {'date': '2013-01-01',
'realtime_end': '9999-12-31',
'realtime_start': '2013-03-21',
'value': '231.222'}]

maxDate = "2013-02-21"

The answer suggested itertools, and it worked for the exact maxDate that's above.
However, when i move the date (say to "2013-01-21") it throws the error:

ValueError: max() arg is an empty sequence.

I can see from the list that there are elements
that have realtime_start values that are lower than 2013-01-21 so this is a bug.

I have read the documents for itertools, but cannot quite work out this groupby stuff.

there's a snip of the SO solution below -- help understanding as well as the bug fix would be much appreciated.

thanks in advance,

Matt Johnson


You should refer to my earlier answer using groupby -- where
I sorted the list before using groupby on it; you can read some
notes on this in python groupby documentation.

I'm not sure why you insist on using groupby for this problem.

I would also try to stay away from packing so much in a single
list comp as the SO answer does, and instead split things into
separate steps where each step is simple and clear, then
you'd be able to print out intermediate results and see what
exactly is happening.

 -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to