## Distance to the nearest non-zero value in a list?

python list closest to zero

python find two closest value in list

find closest number to zero in array java

closest to zero javascript

java find closest number in list

closest to zero c#

close to zero python

I have list like this one:

[0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]

and I'd like to get a list that describes the distance to closest non-zero value for each element. In this case it would be:

[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

Initialize the list of distances with infinity.

Loop over the list and keep track of the distance to the last-seen non-zero value. If the distance is smaller than the distance stored in the list (currently infinity), update the

`distances`

list.Repeat step 2 but iterate over the list backwards.

Code:

lst = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] # initialize the distances to infinity distances = [float('inf')] * len(lst) # loop over the list twice, once from the left and once from the right for indices in [range(len(lst)), range(len(lst)-1, -1, -1)]: # keep track of the distance to the last seen non-zero value distance = float('inf') for i in indices: # when we find a non-zero value, set the distance to 0 if lst[i] != 0: distance = 0 distances[i] = min(distances[i], distance) # with each step we take, increase the distance by 1 distance += 1 print(distances) # output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

Given an array of n integers, for each element, print the distance to the closest zero. element till we find the 0, and in worst case we may not find in one direction. Now we should store the minimum of current value of distance and value Find nearest value greater than. Select a blank cell which you will put the lookup result, type this formula =MIN(IF(A1:B6>E1,A1:B6)), and press Shift + Ctrl + Enter keys simultaneously. See screenshot: In the formula, A1:B6 is the range you want to find value, E1 is the value you look up.

import numpy as np test = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] non_zero_idx = np.array([idx for idx, x in enumerate(test) if x is not 0])

At this point you have the idx of all values that are non-zero. All you have to do is take the minimum between the difference of that and the idx of the value for each value.

distances = [min(abs(idx-np.array(non_zero_idx))) for idx, x in enumerate(test)] distances

[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

j)th entry in the array is non-zero for the corresponding feature pixel in the to specify for each pixel location (i,j) the distance to the nearest non-zero pixel of A or value of match for this combination iflow value add to lowest list end-for Step 3 To find the closest match with a lookup value and numeric data, you can use an array formula based the INDEX, MATCH, ABS and MIN functions. In the example shown, the formula in E5 is: where "data" is the named range B5:B14, and E4 contains a lookup value. Note: this is an array formula and must be entered with control + shift + enter.

You can use two list comprehensions to assemble the distances marching forward and backward through the list:

s = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1] forward = [s[i:].index(1) if x!=1 else 0 for i, x in enumerate(s)] s_r = s[::-1] backward = [s_r[i:].index(1) if 1 in s_r[i:] else 0 if x==1 else float('inf') for i, x in enumerate(s_r)][::-1]

Then you can zip the two resulting lists together and take the minimum distance at each index:

final = [min(i,j) for i, j in zip(forward, backward)]

Yields:

[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

In a For loop I use each loop the next row for calculations, but for the last column I want to select the closest non zero value in that column. So this solution might come up with, say, row 40 as the closest distance, but my solution might come up with a closer distance between row 34 or N with row 53 of V. If you have the Statistics and Machine Learning Toolbox, you can also compute the distance between every element and every element of the other array using the function pdist2().

def f(lst): ans = [0 for x in range(len(lst))] last_non_zero = -len(lst) last_edited = -1 for i in range(len(lst)): if lst[i] != 1: for j in range(last_edited+1, i): ans[j] = min(abs(i-j), abs(j-last_non_zero)) last_edited = i last_non_zero = i return ans

The time complexity is O(n).

I have an array with x amount of values. How can I find the index value of the element that is closest or equal to a certain value? I tried it in the following manner, In Matlab, I have a matrix, where some elements are set to zero. E.g: 0 1 0 0 0 2 5 0 3 0 0 0 0 0 0 0 5 0 2 1 For every matrix element which is zero I want to find the indices of the nearest elem

I really like your idea of splitting the list -- it's very functional! Let's look at optimizing that approach, though. def nearest_non_zero(lst, idx): I have a List, MyStuff has a property of Type Float. There are objects with property values of 10,20,22,30. I need to write a query that finds the objects closest to 21, in this case it would find the 20 and 22 object. Then I need to write one that finds the object closes to 21 without going over, and it would return the object with a value of 20.

However, the highest similarity is not bounded, so every nonzero value is divided by to transform the distances between the query and its L-nearest-neighbors (rank Using this method, a new ranking list is build inserting first the reciprocal Manhattan Distance: We use Manhattan Distance if we need to calculate the distance between two data points in a grid like path. As mentioned above, we use Minkowski distance formula to find Manhattan distance by setting p’s value as 1. Let’s say, we want to calculate the distance, d, between two data points- x and y.

from each background pixel to the object pixel closest to it. exact Euclidean distances of background pixels to the object pixels closest to R and C are two lists of size N keeping the row and column numbers of the At this point, the object pixels in image a] ] have nonzero labels and the background pixels have value 0. Nearest[{elem1, elem2, }, x] gives the list of elemi to which x is nearest. Nearest[{elem1 -> v1, elem2 -> v2, }, x] gives the vi corresponding to the elemi to which x is nearest. Nearest[{elem1, elem2, } -> {v1, v2, }, x] gives the same result. Nearest[{elem1, elem2, } -> prop, x] gives the property prop for the elemi to which x is nearest. Nearest[data, {x1, x2

##### Comments

- What is not working with your attempt?
- It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem individually. A good way to show this effort is to include a Minimal, complete, verifiable example. Check the intro tour you finished before posting, especially How to Ask.
- you might want to generalize to
`if lst[i] != 0`

. he doesnt say the input has to be all 0's and 1's - @stackPusher Thanks!