You can use spatial KDtree in scipy . It uses a fast tree algorithm to determine close points for vectors of arbitrary dimension.
Edit: sorry if you're looking for arbitrary distance metrics , a tree-like structure may still be an option.
Here is an example:
>>> from scipy import spatial >>> A = [[0,1,2,3,4], [4,3,2,1,0], [2,5,3,7,1], [1,0,1,0,1]] >>> tree = spatial.KDTree(A)
This installs KDTree with all points in A, which allows you to perform quick spatial searches in it. Such a request takes a vector and returns the nearest neighbor in A:
>>> tree.query([0.5,0.5,0.5,0.5,0.5]) (1.1180339887498949, 3)
The first return value is the distance of the nearest neighbor, and the second is its position in A, so you can get it, for example, as follows:
>>> A[ tree.query([0.5,0.5,0.5,0.5,0.5])[1] ] [1, 0, 1, 0, 1]