Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use <code>itertools.groupby</code>:</p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; # sort so that the same dates are contiguous &gt;&gt;&gt; obs.sort(key=lambda x: x['date']) &gt;&gt;&gt; grouped = itertools.groupby(obs, lambda x: x['date']) &gt;&gt;&gt; m = [max(g, key=lambda x: x['realtime_start']) for k, g in grouped] &gt;&gt;&gt; &gt;&gt;&gt; import pprint &gt;&gt;&gt; pprint.pprint(m) [{'date': '2012-10-01', 'realtime_end': '9999-12-31', 'realtime_start': '2012-12-19', 'value': '231.623'}, {'date': '2012-11-01', 'realtime_end': '9999-12-31', 'realtime_start': '2013-01-19', 'value': '231.071'}, {'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-03-21', 'value': '231.222'}] </code></pre> <p>You can add other conditions too:</p> <pre><code>&gt;&gt;&gt; grouped = itertools.groupby(obs, lambda x: x['date']) &gt;&gt;&gt; m = [max((w for w in g if w['realtime_start'] &lt;= maxDate), key=lambda x: x['realtime_start']) for k, g in grouped] &gt;&gt;&gt; pprint.pprint(m) [{'date': '2012-10-01', 'realtime_end': '9999-12-31', 'realtime_start': '2012-12-19', 'value': '231.623'}, {'date': '2012-11-01', 'realtime_end': '9999-12-31', 'realtime_start': '2013-01-19', 'value': '231.071'}, {'date': '2012-12-01', 'realtime_end': '9999-12-31', 'realtime_start': '2013-02-19', 'value': '231.137'}, {'date': '2013-01-01', 'realtime_end': '9999-12-31', 'realtime_start': '2013-02-21', 'value': '231.198'}] </code></pre> <p>But I'd recommend looking at my favourite Python data manipulation library, <a href="http://pandas.pydata.org/" rel="nofollow">pandas</a>: it's great for tabular and time-series data, and data manipulation using it would be much easier (and more R-like in functionality) than anything you could roll yourself. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload