[Tutor] Clustering?

Andre Engels andreengels at gmail.com
Tue Jan 16 16:35:20 CET 2007

2007/1/16, Carlos <carloslara at web.de>:
> Hello to everybody,
> I have a question that I think is a little related to clustering, I have
> a list of lists, like this:
> List = [[1,5],[6,8],[48,10],[99,56]]
> The list is composed by a large number of lists not just four, and each
> 'interior' list contains two numbers that are the location of an object
> in a plane, so they are X and Y coordinates, my question is:
> Can I use this list to evaluate how many points are in a given range?
> Thats is taking the highest and lowest values for X an Y and, lets say
> divide that range in 10 regions, then get the number of objects on each
> region. Is this possible? and if yes, how canI do it? I ask this because
> as you know my python skills are not that great :)

First, this feels like a list of tuples rather than a list of lists;
however, tuples and lists don't differ that much in their behaviour, so
there's nothing really lost.

And yes, it is possible. An inline if would be the way I would resolve that:

def withinrange(list,xmin,xmax,ymin,ymax):
    # Get the elements of list for which the first part of the pair is
between xmin and xmax
    # (inclusive) and the second between ymin and ymax.
    return [c for c in list if xmin <= c[0] <= xmax and ymin <= c[1] <=

The longer but clearer method of doing the same would be:

def withinrange(list,xmin,xmax,ymin,ymax):
    templist = []
    for c in list:
        if xmin <= c[0] <= xmax and ymin <= c[1] <= ymax:
    return templist

Andre Engels, andreengels at gmail.com
ICQ: 6260644  --  Skype: a_engels
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070116/6a460e22/attachment.htm 

More information about the Tutor mailing list