in place list modification necessary? What's a better idiom?
MRAB
google at mrabarnett.plus.com
Tue Apr 7 07:12:20 EDT 2009
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]))
More information about the Python-list
mailing list