[Tutor] Help with iterators

Steven D'Aprano steve at pearwood.info
Fri Mar 22 03:05:38 CET 2013


On 22/03/13 11:39, Matthew Johnson wrote:
> Dear list,
>
> I have been trying to understand out how to use iterators and in
> particular groupby statements.  I am, however, quite lost.

groupby is a very specialist function which is not very intuitive to
use. Sometimes I think that groupby is an excellent solution in search
of a problem.


> I wish to subset the below list, selecting the observations that have
> an ID ('realtime_start') value that is greater than some date (i've
> used the variable name maxDate), and in the case that there is more
> than one such record, returning only the one that has the largest ID
> ('realtime_start').


The code that you show does not so what you describe here. The most
obvious difference is that it doesn't return or display a single record,
but shows multiple records.

In your case, it selects six records, four of which have a realtime_start
that occurs BEFORE the given maxDate.

To solve the problem you describe here, of finding at most a single
record, the solution is much simpler than what you have done. Prepare a
list of observations, sorted by realtime_start. Take the latest such
observation. If the realtime_start is greater than the maxDate, you have
your answer. If not, there is no answer.

The simplest solution is usually the best. The simpler your code, the fewer
bugs it will contain.


obs.sort(key=lambda rec: rec['realtime_start'])
rec = obs[-1]
if rec['realtime_start'] > maxDate:
     print rec
else:
     print "no record found"


which prints:

{'date': '2013-01-01', 'realtime_start': '2013-03-21', 'realtime_end': '9999-12-31', 'value': '231.222'}




-- 
Steven


More information about the Tutor mailing list