Jono - 5 months ago 11x

Python Question

I'm relatively new to programming and math, so forgive me if I'm not asking this question in the right way. Basically I have a range of dates, like this:

`dateList = ['1818', '1813', '1818', '1811', '1813']`

and I want to plot these eventually, but the plot function needs values between, say, 30 and 80 in order to look good. That means the min of this set, 1811, should be at 30, and the max, 1818, should be at 80. Is there a way to write a function fancyMap such that:

`fancyMap(dateList, (30, 80))`

outputs something like (guessing here):

`[80, 52, 80, 30, 52]`

I know there's some simple math involved here, but I'm not sure what it is.

Answer

As an addition to Claris' answer, this function works for different bounds:

```
def fancyMap(array, low, high):
array = [int(numeric_string) for numeric_string in array]
minimum = min(array)
maximum = max(array)
diff = maximum - minimum
diffScale = high - low
return map ( lambda x: int((int(x)-minimum)*(float(diffScale)/diff)+low), array)
```

Source (Stackoverflow)

Comments