1

I have a list of numbers that I wish to somehow convert to unit step form

These are the boundaries I have

[367.3090551181102, 371.73179133858264, 376.1545275590551, 380.5772637795276, 385.0]

This is the data I have

[367.3090551181102, 370, 370, 370, 370, 370, 370, 370, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 369, 368, 369, 368, 368, 368, 368, 368, 368, 368, 368, 369, 370, 367.3090551181102, 367.3090551181102, 367.3090551181102, 370, 368, 373, 369, 371, 368, 367.3090551181102, 367.3090551181102, 367.3090551181102, 369, 367.3090551181102, 372, 367.3090551181102, 368, 368, ...]

What I want to do is to basically round every number in the data down or up to the boundary. The number is between lower boundary and upper boundary.

Assume that the number is 370, I want to round this to 371.73... since it is between 367.309... and 371.73... or the number is 381, then i want to round it to 370.57...

This is the code I have but i am bit confused

def unit_step(data,boundaries):
    us = []
    for d in data:
        if(boundaries[0] < d < boundaries[1]):
            us.append(rounder(boundaries[0],boundaries[1],d))
        elif(boundaries[1] < d < boundaries[2]):
            us.append(rounder(boundaries[1],boundaries[2],d))
        elif(boundaries[2] < d < boundaries[3]):
            us.append(rounder(boundaries[2],boundaries[3],d))
        elif(boundaries[3] < d < boundaries[4]):
            us.append(rounder(boundaries[3],boundaries[4],d))
    return us

I tried for loop inside another for loop but couldn't make it.

Where am i wrong or is it a useful approach compared to double for loop?

Resul Bulbul
  • 45
  • 1
  • 10
  • Does this answer your question? [from list of integers, get number closest to a given value](https://stackoverflow.com/questions/12141150/from-list-of-integers-get-number-closest-to-a-given-value) – Mady Daby Apr 29 '21 at 19:35
  • @MadyDaby Nope not the one I was looking for – Resul Bulbul Apr 29 '21 at 19:39

1 Answers1

3

You could use a list comprehension with min to check the nearest (higher or lower) value from boundaries and replace the values from data accordingly

>>> [min(boundaries, key=lambda i: abs(i - value)) for value in data]
[367.3090551181102, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 367.3090551181102, 371.73179133858264, 367.3090551181102, 367.3090551181102, 367.3090551181102]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218