# [Tutor] Clustering?

Carlos carloslara at web.de
Tue Jan 16 19:05:03 CET 2007

```Hallo ,

Andre thanks a lot for your help, seems to me like  my script can work

I found this searching the internet:

cluster 1.1.1b2
python-cluster is a "simple" package that allows to create several
groups (clusters) of objects from a list

>>> from cluster import *
>>> data = [12,34,23,32,46,96,13]
>>> cl = HierarchicalClustering(data, lambda x,y: abs(x-y))
>>> cl.getlevel(10)     # get clusters of items closer than 10
[96, 46, [12, 13, 23, 34, 32]]
>>> cl.getlevel(5)      # get clusters of items closer than 5
[96, 46, [12, 13], 23, [34, 32]]

I would like to give it a try because I have the impression that it can
be helpful too. My problem now is the lambda function, I was wondering
if someone could be so kind as to give me an example that could work in
my list with nested tuples.

Thanks again!
Carlos

Andre Engels wrote:
> 2007/1/16, Carlos <carloslara at web.de <mailto: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]
> <= ymax]
>
> 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:
>             templist.append(c)
>     return templist
>
> --
> Andre Engels, andreengels at gmail.com <mailto:andreengels at gmail.com>
> ICQ: 6260644  --  Skype: a_engels
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.432 / Virus Database: 268.16.12/630 - Release Date: 1/15/2007 8:28 PM
>

```