Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

Quickly Find the Index in an Array Closest to Some Value

This seems much quicker (for me, Python 3.2-win32, numpy 1.6.0):

from bisect import bisect_left
def f3(t, x):
    i = bisect_left(t, x)
    if t[i] - x > 0.5:
        i-=1
    return i

Output:

[   10    11    12 ..., 99997 99998 99999]
37854.22200356027
37844
37844
37844
37854
37854
37854
f1 0.332725
f2 1.387974
f3 0.085864

Tags:

Python

Performance

Indexing

Numpy

Related

Unicode to PHP exec Executing a shell command from Common Lisp FileSystemWatcher Class - Excluding Directories Background image doesn't show when defined in stylesheet MySQL select all rows from last month until (now() - 1 month), for comparative purposes SQL Server 2008 - How to convert GMT(UTC) datetime to local datetime? Get angle from 2 positions How to get IP address of the device from code? Xcode 4 project: utility to clean up pbxproj file? Can an SVG vector graphic be converted to a PNG file format for use in Fireworks? Can I find the MAC address of my Access point in Android? matplotlib colorbar for scatter

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy