[Tutor] scaling values

kevin parks kp8 at mac.com
Wed Mar 15 00:03:40 CET 2006


Seems my post added much confusion. Sorry... I was hoping not to have 
to post my code since it is really wrong and slightly embarrassing.. 
what i am trying to do is map an input range of values to output range. 
I was hoping to make it a bit of an all purpose utility that would map 
pretty much any input range to an output range, also do inverted 
mapping... and also handle negative numbers and perhaps even a flag for 
exponential mapping.

import random

def scaleX(in_seq, low, hi):
	range1 = max(in_seq) - min(in_seq)
	#range2 = max(out_seq) - min(outseq)
	range2 = hi - low
	ratio = range1/range2
  	return [(x * ratio) for x in in_seq]		

def test():
	# Create a list of 15 random integers in the range 0 to 127
	# see if we can map it to 0 -> 1
	inseq = random.sample(xrange(128), 25)
	print scaleX(inseq, 0.0, 1.0)

if __name__ == "__main__":


More information about the Tutor mailing list