python - Given a float, find if in list or closest number to it -
I have a list of Tuplesses, that is, with the values of X and Y, 2-dim tuples call it data two floats , Xmin and xmx, and want to withdraw the indicator of the maximum Y value at that interval. It should also work, even if XMan and XMX do not match the exact number of data.
I understand how XML and XMX should be a problem apart from rounding the closest values of the list. I have no idea because I'm a dragon newbie.
Assign the pointer of points (x, y) to maximum interval [xmin, xmax] def find_peak (data, xmin, xmx): Temporarily, I can search through the list and record the smallest difference for each x-value. Is it possible or is there a clever way?
Looking at the list of 2 dimensional coordinates.
- Sort the data based on the x coordinate should be the natural list sort.
- Use the python module to determine the starting and ending index of data points
- Use with the built in
minimum key as Sample Implementation def foo (data, x_min, x_max): bisect import bisect_left, biscact data from operator import item = sorted (data) x_data = [x, y for x date Index_min = bisect_left (x_data, x_min) index_max = bisect (x_data, x_max) return maximum (data [index_min: index_max], key = itemgetter (1)) [- 1] Example run & gt; & Gt; & Gt; Data = [[(random.randint (1,20), random.randint (1,20)) _ in the category (10)]> gt; & gt; Figures [(9, 9), (11, 11), (7, 7), (16, 11), (15, 19), (8, 18), (16, 3), (18, 7) (17, 13), (3, 11) ]> Gt; & gt; Foo (Data, 3,7) 11
Comments
Post a Comment