[Numpy-discussion] 2D binning

Keith Goodman kwgoodman@gmail....
Tue Jun 1 15:22:33 CDT 2010

On Tue, Jun 1, 2010 at 1:07 PM, Mathew Yeates <mat.yeates@gmail.com> wrote:
> Hi
> Can anyone think of a clever (non-lopping) solution to the following?
> A have a list of latitudes, a list of longitudes, and list of data values.
> All lists are the same length.
> I want to compute an average  of data values for each lat/lon pair. e.g. if
> lat[1001] lon[1001] = lat[2001] [lon [2001] then
> data[1001] = (data[1001] + data[2001])/2
> Looping is going to take wayyyy to long.

Looping N and searching in N would be O(N^2). But would it be too long
just to loop, so O(N)? If you need to find all averages you could try
something like this:

from collections import defaultdict

def aggdict(x):
    "Convert [(1, 'a'), (2, 'b'), (1, 'A')] to {1: ['a', 'A'], 2: ['b']}."
    d = defaultdict(list)
    for k, v in x:
    return dict(d)

More information about the NumPy-Discussion mailing list