[Tutor] Clustering?

Alan Gauld alan.gauld at btinternet.com
Wed Jan 17 00:28:14 CET 2007


"Carlos" <carloslara at web.de> wrote

>>> 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]]

> 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.

lambda is just a shorthand way of writing a simple function.
You don't need to use lambda. The above line could have
been done thisaway:

>>> def f(x,y): return abs(x-y)
>>> cl = HierarchicalClustering(data, f)

So provided you can write a function to do what you want you
don't need the lambda if it confuses you.

In a general sense:

def f(p): return expression

is the same as

f = lambda p: expression

So anywhere that a function name is nmeeded you can
put a lambda.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list