我有一個(gè)清單。我想找到最接近給定數(shù)字的值。一切正常。但問題是,它產(chǎn)生nan兩個(gè)唯一數(shù)字的輸出。在這里我提供我的完整數(shù)據(jù)我的代碼和輸出:#### 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 ### 預(yù)期輸出:print(rse_st,rse_ed)0.172816,0.008605
1 回答

波斯汪
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
用途nanargmin
:
def?find_nearest(array,?value): ????array?=?np.asarray(array) ????idx?=?np.nanargmin(np.abs(array?-?value)) ????????return?array[idx]
添加回答
舉報(bào)
0/150
提交
取消