Carl Banks wrote:
MooMaster wrote:
So I'm reading in values from a file, and for each column I need to
dynamically discover the range of possible values it can take and
quantize if necessary. This is the solution I've come up with:

[snip]
#harvested from http://www.rosettacode.org/wiki/IsNumeric#Python
def isNumeric(string):
    try:
        i = float(string)
    except ValueError:
        return False
    return True

[snip]

import collections
import itertools

def createInitialCluster(fileName):
    fixedPoints = []
    # quantization is a dict that assigns sequentially-increasing
numbers
    # to values when reading keys that don't yet exit
    quantization = defaultdict.collections(itertools.count().next)
    with open(fileName, 'r') as f:
        for line in f:
            dimensions = []
            for s in line.rstrip('\n').split(","):
                if isNumeric(s):
                    dimensions.append(float(s))
                else:
                    dimensions.append(float(quantization[s]))
            fixedPoints.append(Point(dimensions))
    return Cluster(fixedPoints)

I'd also remove the isNumeric() function. You're just converting to a
float if you can successfully convert to a float!

                try:
                    dimensions.append(float(s))
                except ValueError:
                    dimensions.append(float(quantization[s]))
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to