thraxil.org:

Reply to: A Simple Programming Puzzle Seen Through Three Different Lenses

You can knock off a list traversal:

def cluster_by(f, lst):
d = dict()
for x in lst:
key = f(x)
d.setdefault(key,[]).append(x)
return d.values()

And use a generator instead of a list comprehension:

clustered = cluster_by(normalize,((x,y) for x in states for y in states))


formatting is with Textile syntax. Comments are not displayed until they are approved by a moderator. Moderators will not approve unless the comment contributes value to the discussion.

namerequired
emailrequired
url
remember info?