Python Forum

Full Version: Trying to find an approximate match in a Column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here
Hi everyone,

I've just started learning Python so no idea about 'how to find an approximate match of a value in Integer type of column'
my code here
Please read this
you need to give a lot more information on what you are trying to do!
Hi Sir,

So I have a column of integer values and generating random number. Now I need to find an exact or approximate match of that Random Number.

For Example:
Generated Random Number=55355
Exact match=55355
but when exact match is not in the data then we'll find approximate match and it can be 55280.

Kindly help me solving this and many thanks in advance.

Regards,
Ankur K
If you are using v 3.5 +, you can try out math.isclose()
math.isclose() can be helpful. It also lets you define the degree of approximation(relative and absolute tolerance).

(Sep-02-2017, 01:02 PM)ankur2207 Wrote: [ -> ]So I have a column of integer values and generating random number. Now I need to find an exact or approximate match of that Random Number.

But if you want to find the closest match in a list of the number then you can use:
closest = min(random_list, key=lambda x:abs(x-random_num))