我有一个清单。我想找到最接近给定数字的值。一切正常。但问题是,它产生nan两个唯一数字的输出。在这里我提供我的完整数据我的代码和输出:#### Find the index of nearest value in a arraydef find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() return array[idx] #for returing nearest value r = [0.209272 , 0.172816 , 0.1297975 , 0.0777895 , 0.008605 , np.nan, np.nan, np.nan, np.nan, np.nan]rc = 0.38420566666666667rse_ed = find_nearest(r,0.01) ### close to 0.01 is 0.008605rse_st = find_nearest(r,rc*0.4) #### close to 0.15368226666666668 is 0.172816print(rse_st,rse_ed)nan,nan ### 预期输出:print(rse_st,rse_ed)0.172816,0.008605
1 回答
波斯汪
TA贡献1811条经验 获得超4个赞
用途nanargmin
:
def find_nearest(array, value): array = np.asarray(array) idx = np.nanargmin(np.abs(array - value)) return array[idx]
添加回答
举报
0/150
提交
取消