Mapping a value in python

From XPUB & Lens-Based wiki
Revision as of 13:06, 20 March 2017 by Michael Murtaugh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
def map (v, inlow, inhigh, outlow, outhigh):
    p = (float(v) - inlow) / inhigh
    return outlow + (p * (outhigh - outlow))

Example: Maps the input value 50 from an input range of 0 - 100 to an output range of 2000 and 3000.

 map(50, 0, 100, 2000, 3000)
 Out[2]: 2500.0