Python Forum
finding the closest floating point number in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding the closest floating point number in a list
#1
i am not asking for anyone to code this for me. i'm just wanting to know if there might be an already existing tool out there to do this. otherwise i'll code it myself.

i have a big list of tuples. the first item in each tuple is a floating point number. the remaining items in each tuple are items with association to that first item. the list is sorted. there may be tuples with exactly the same first item, and the order of these does not matter.

i need a function that is given a floating point number and with that number, needs to find the tuple with the closest first item. if adjacent tuples are exactly the same, the function needs to return a list of all that have exactly identical first items that meet the requirement of being closest.

i might redo this all using decimal.Decimal type instead of float.

speed is not important. the lookup will be rarely done, maybe a few dozen times at boot-up and once every few hours thereafter. if it takes a couple seconds to run, no big deal. the list will be just a few thousand in length at most.

i've imagined a few ways to optimize this lookup. but this is not needed in this case. sequential is sufficient.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Wouldn't the bisect module suit your needs?
Reply
#3
perhaps it would. i'd have to construct the list that way. but it is also overkill, since the list is completed and then sorted before the one search is needed. one of my ideas was to select the closer values above and below as the numbers were loaded in the order they are created (not sorted). each time a number is closer, it replaces the previous number, done for above and done separately for below. with that, i would not have to store the numbers or do a sort.

the bisect module looks like it may be useful for another project. this will walk its way through the entire file tree of a file system of any size and keep the last {os.environ['ROWS']} largest files in a list sorted in largest-last order. if there is a way to remove items from a bisect object, then i can keep a finite size list by removing the smallest item before adding a new one each time. i just need to find a bisect.delete_lowest() method.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
An interesting thin wrapper above bisect is the ActiveState recipe SortedCollection. It does implement the CRUD capabilities that you're talking about. Have a look at it. Also a module such as sortedcontainer could be relevant.
Reply
#5
Hi Skaperen, what do you think about this?

import random
import numpy as np

# create a big list of tuples with a float as first column
big_list_of_tuples = [(random.random(), 1, 2, 3, "A", "B") for i in range(10000)]

# make three entries the same
big_list_of_tuples[4999] = big_list_of_tuples[5000]
big_list_of_tuples[5001] = big_list_of_tuples[5000]

# we are searching for the float of that three entries
value = big_list_of_tuples[5000][0]
print(value)

# create a numpy array out of the tuple list
array = np.asarray(big_list_of_tuples, dtype=np.object)

# create an array with the first column being real floats
floats = array[:, 0].astype(np.float)

# calculate the difference of the floats and the float we are looking for
difference = (np.abs(floats - value))

# use argmin() to find the index of the first closest value
closest = difference.argmin()
print(closest)

# let´s look if there are other entries with the same difference
identical = difference == difference[closest]

# use this mask as a view into the array
print(array[identical])
Reply
#6
I am not a math guy but simple extraction should work. You just get the smallest absolute result.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
wavic Wrote:I am not a math guy but simple extraction should work. You just get the smallest absolute result.
This is true if the set of numbers is unordered, you don't have anything better than a 0(n) algorithm. But if it is a sorted list of data, as was implied by Skaperen's first post, binary search has complexity O(log2(n)). That's why I suggested bisect.

Numpy also has a searchsorted function that could perhaps be used.
Reply
#8
@ThomasL: i'm lost at line 19 in that code.

i will have to look at numpy, then.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
That´s just one column with all the float values, all the first values in your tuples
Reply
#10
i'm going to need to find some extra time to learn numpy. then i will try regex again. but it will never try again to learn perl or lisp ... never, ever!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error is finding mean of a list PythonBoy 4 885 Sep-11-2023, 02:38 PM
Last Post: PythonBoy
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,181 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Finding combinations of list of items (30 or so) LynnS 1 867 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Finding the price based on industry and number of transactions chandramouliarun 0 906 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,846 Jul-01-2022, 01:23 PM
Last Post: deanhystad
Question Finding string in list item jesse68 8 1,858 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Split a number to list and list sum must be number sunny9495 5 2,276 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,012 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  finding point in m grid jenya56 0 813 Feb-06-2022, 09:00 PM
Last Post: jenya56

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020